0

I'm building a simple Dash app, including an html.Video() component. The issue is that local video files aren't playing (videos hosted online are working fine).

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)

Folder structure:

  • app.py
  • /static
    • test.mp4

I use OpenCV to create the .mp4 files:

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

I've tried the solutions from here, here, here, and here, without any luck

1 Answer 1

0

Figured out the issue. For whatever reason, the .mp4 videos created by OpenCV using ".mpv4" is not compatible with the html.Video() component in Dash. Using ffmpeg to create the video worked as expected. Hope it saves someone a few hours in the future.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.