上传和播放视频烧瓶有问题吗?

problem to upload and play a video flask?

我正在尝试上传视频并使用 Flask 在 html 页面中播放,但问题是它无法播放。我使用了以下代码:

UPLOAD_FOLDER = 'static/'

@app.route('/')
def upload_form():
    return render_template('upload.html')


@app.route('/', methods=['POST'])
def upload_video():
    if 'file' not in request.files:
        flash('No file part')
        return redirect(request.url)
    file = request.files['file']
    if file.filename == '':
        flash('No image selected for uploading')
        return redirect(request.url)
    else:
        filename = secure_filename(file.filename)
        file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
        # print('upload_video filename: ' + filename)
        flash('Video successfully uploaded and displayed below')
        return render_template('upload.html', filename=filename)


@app.route('/display/<filename>')
def display_video(filename):
    # print('display_video filename: ' + filename)
    return redirect(url_for('static', filename=filename), code=301)
if __name__ == '__main__':
    app.run(threaded=True)

在 html 文件中:

<p>
    {% with messages = get_flashed_messages() %}
      {% if messages %}
        <ul>
        {% for message in messages %}
          <li>{{ message }}</li>
        {% endfor %}
        </ul>
      {% endif %}
    {% endwith %}
</p>
{% if filename %}
    <div style="margin: 10px auto;">
        <video autoplay="autoplay" controls="controls" preload="preload">
            <source src={{ url_for('display_video', filename=filename) }} type="video/mp4"></source>
        </video>
    </div>
{% endif %}
<form method="post" action="/" enctype="multipart/form-data">
    <dl>
        <p>
            <input type="file" name="file" autocomplete="off" required>
        </p>
    </dl>
    <p>
        <input type="submit" value="Upload">
    </p>
</form>

视频已上传但无法播放,我无法播放播放按钮或与之互动

我认为您应该使用 send_from_directory 使文件可访问。

@app.route("/display/<path:filename>")
def display_video(filename):
    return send_from_directory(
        app.config['UPLOAD_FOLDER'], filename, as_attachment=True
    )

我还在source标签中加了引号。

<video autoplay="autoplay" controls="controls" preload="preload">
  <source src="{{ url_for('display_video', filename=filename) }}" type="video/mp4"></source>
</video>