无法在 Dash 中播放本地视频文件
Can't play local video file in Dash
我正在构建一个简单的 Dash 应用程序,包括一个 html.Video() 组件。问题是本地视频文件无法播放(在线托管的视频工作正常)。
import dash
from dash.dependencies import Input, Output
import os
app = dash.Dash(__name__)
app.layout = html.Video(src="/static/test.mp4", controls=True)
@app.server.route('/static/<path:path>')
def serve_static(path):
root_dir = os.getcwd()
return flask.send_from_directory(os.path.join(root_dir, 'static'), path)
if __name__ == '__main__':
app.run_server(debug=True)
文件夹结构:
- app.py
- /静态
- test.mp4
我使用 OpenCV 创建 .mp4 文件:
def crop_video(vid_file, start_frame, end_frame, fps=30.0):
vid_name = f"/static/test.mp4"
cap = cv2.VideoCapture(vid_file)
ret, frame = cap.read()
h, w, _ = frame.shape
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
writer = cv2.VideoWriter(vid_name, fourcc, fps, (w, h))
f = 0
while ret:
f += 1
if start_frame <= f and f <= end_frame:
writer.write(frame)
ret, frame = cap.read()
writer.release()
cap.release()
return vid_name
解决了问题。无论出于何种原因,OpenCV 使用“.mpv4”创建的 .mp4 视频与 Dash 中的 html.Video() 组件不兼容。使用 ffmpeg 创建视频按预期工作。希望它能在未来为某人节省几个小时。
我正在构建一个简单的 Dash 应用程序,包括一个 html.Video() 组件。问题是本地视频文件无法播放(在线托管的视频工作正常)。
import dash
from dash.dependencies import Input, Output
import os
app = dash.Dash(__name__)
app.layout = html.Video(src="/static/test.mp4", controls=True)
@app.server.route('/static/<path:path>')
def serve_static(path):
root_dir = os.getcwd()
return flask.send_from_directory(os.path.join(root_dir, 'static'), path)
if __name__ == '__main__':
app.run_server(debug=True)
文件夹结构:
- app.py
- /静态
- test.mp4
我使用 OpenCV 创建 .mp4 文件:
def crop_video(vid_file, start_frame, end_frame, fps=30.0):
vid_name = f"/static/test.mp4"
cap = cv2.VideoCapture(vid_file)
ret, frame = cap.read()
h, w, _ = frame.shape
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
writer = cv2.VideoWriter(vid_name, fourcc, fps, (w, h))
f = 0
while ret:
f += 1
if start_frame <= f and f <= end_frame:
writer.write(frame)
ret, frame = cap.read()
writer.release()
cap.release()
return vid_name
解决了问题。无论出于何种原因,OpenCV 使用“.mpv4”创建的 .mp4 视频与 Dash 中的 html.Video() 组件不兼容。使用 ffmpeg 创建视频按预期工作。希望它能在未来为某人节省几个小时。