python - Flask 不会在 html 中播放视频

标签 python html video flask

我有一个 flask 应用程序,它应该在页面加载时播放视频,但它只是显示在左上角,而不是从视频的第一帧改变

我已经尝试将它插入到 html 代码中,但它不起作用

{% extends "base.html"  %}


{% block content %}

<video height="224" width="400" preload="auto" data-video-width="400"  src="../static/chugging.mp4" style="margin-right: auto; margin-left: auto; align-items: center"></video>

{% endblock %}

我希望看到它播放或至少看到某个播放按钮,但它只是左上角的一个小框架

最佳答案

在 Flask 模板中播放视频

如果您在 static 文件夹中有视频文件,您可以像其他静态文件一样使用 url_for 在模板中访问它们。可以找到提供静态文件的文档 in this URL .

这里我展示了一个在模板中播放视频文件的例子。

目录结构:

├── app.py
├── static
│   └── demo.mp4
└── templates
    └── index.html

app.py:

from flask import Flask, render_template


app = Flask(__name__, static_folder='static')

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

index.html:

<!DOCTYPE html>
<html>
<head>
  <title>Video Example</title>
</head>
<body>
  <h2>Serving video files from Flask template</h2>
  <video width="320" height="240" controls>
    <source src={{ url_for('static', filename="demo.mp4") }} type="video/mp4">
    Your browser does not support the video tag.
  </video>
</body>
</html>

输出:

video playing in Flask template

关于python - Flask 不会在 html 中播放视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55961665/

相关文章:

python - 使用 Lambda 和元组发送到多个函数 : Python, Tkinter

python - 使用枚举的 Tkinter 下拉选项菜单

javascript - 恢复视频播放 JavaScript

python - 用于约束参数的卡方检验

python - 此批处理文件中只有一行将运行

javascript - $ ('script' ).detach(); 的影响

html - :target pseudo selector not selecting target

php - 用户个人资料图像和内容之间的空间。 Bootstrap

javascript - 一个视频资源是否可以在多个 3D 平面上渲染,所有平面均以 webVR 中的不同 video.currentTime 值开始?

.net - 2 个视频的 FFMPEG 覆盖问题,第二个输入中缺少声音