问题
I am trying to embed a YouTube video in an iframe on a Flask server hosted on a Raspberry Pi. The issue happens whenever I try to play the video in the iframe. A majority of videos say "Video Not Available" with no further explanation. To further confuse me, not only are the videos that create this error message able to be embedded (the video uploader can select whether or not they want the video to be available for embedding or not), but also there are videos for which this problem does not happen. The most common solution I have seen is "just add 's' to the embed URL's 'http' portion", which does not work for me. Does anyone know what the problem might be?
Edit: Here is the code:
from flask import Flask
from string import Template
HTML_TEMPLATE = Template("""
<h2>
YouTube video link:
<a href="https://www.youtube.com/watch?v=${youtube_id}">
${youtube_id}
</a>
</h2>
<iframe src="https://www.youtube.com/embed/${youtube_id}" width="853" height="480" frameborder="0" allowfullscreen></iframe>""")
app = Flask(__name__)
@app.route('/')
def homepage():
vidhtml = HTML_TEMPLATE.substitute(youtube_id='YQHsXMglC9A')
return """<h1>Hello world!</h1>""" + vidhtml
@app.route('/videos/<vid>')
def videos(vid):
return HTML_TEMPLATE.substitute(youtube_id=vid)
if __name__ == '__main__':
app.run(debug=True, use_reloader=True)
来源:https://stackoverflow.com/questions/53286543/youtube-iframe-embed-video-not-available