Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Play local videos #3

Open
yichengtan opened this issue Feb 24, 2022 · 7 comments
Open

Feature Request: Play local videos #3

yichengtan opened this issue Feb 24, 2022 · 7 comments

Comments

@yichengtan
Copy link

While st.video works for local videos, it is very barebones in terms of features such as getting playback time and video parameters. Adding local video support for this would be great to utilize these features.

@Franky1
Copy link

Franky1 commented Mar 11, 2022

I also was very confused, because the landing page here suggests that "Local Files" are supported, but i could not get it working.

import streamlit as st
from streamlit_player import st_player

# official video player works:
st.video('video.mp4')

# this does not work:
st_player('video.mp4')

@ajaichemmanam
Copy link

Seting up a local server and using it as

st_player("http://localhost:8520/archery.mp4",playing=True,playback_rate=1,progress_interval=1000, controls=True, loop=True)

works.

@Franky1
Copy link

Franky1 commented Mar 29, 2022

@ajaichemmanam
Yes, i know this is a possible solution, but it is way too much complicated and it won't work on streamlit cloud.
Furthermore you cannot use this solution with uploaded videos within the streamlit app.
Therefore this is not a viable solution...

@okld
Copy link
Owner

okld commented May 12, 2022

You can provide custom src and type as URL to ReactPlayer, which allows you to play a local video encoded in base64.

The following sample reads your video locally, encodes it in base64, and builds a valid data URI with the encoded data that can be played by ReactPlayer.

from base64 import b64encode
from pathlib import Path
from streamlit_player import st_player

def local_video(path, mime="video/mp4"):
    data = b64encode(Path(path).read_bytes()).decode()
    return [{"type": mime, "src": f"data:{mime};base64,{data}"}]

st_player(local_video("path/to/video.mp4"))

@vivekdse
Copy link

Seting up a local server and using it as

st_player("http://localhost:8520/archery.mp4",playing=True,playback_rate=1,progress_interval=1000, controls=True, loop=True)

works.

Do you recommend any media server to do this job locally?

@vivekdse
Copy link

You can provide custom src and type as URL to ReactPlayer, which allows you to play a local video encoded in base64.

The following sample reads your video locally, encodes it in base64, and builds a valid data URI with the encoded data that can be played by ReactPlayer.

from base64 import b64encode
from pathlib import Path
from streamlit_player import st_player

def local_video(path, mime="video/mp4"):
    data = b64encode(Path(path).read_bytes()).decode()
    return [{"type": mime, "src": f"data:{mime};base64,{data}"}]

st_player(local_video("path/to/video.mp4"))

This works, which is the good news...but it is very slow with large media files. Need to figure a way to stream the file rather than load it the whole way.

@Stinosko
Copy link

Using flask to serve the local files works for larger files but it is still not perfect.

import os
from flask import Flask, send_file, make_response


APP = Flask(__name__)
MEDIA_PATH = 'path_to_media_folder'

@APP.route('/<path:vid_name>')
def serve_video(vid_name):
    print(vid_name)
    vid_path = os.path.join(MEDIA_PATH, vid_name)
    resp = make_response(send_file(vid_path, 'video/mp4'))
    resp.headers['Content-Disposition'] = 'inline'
    return resp


if __name__ == '__main__':
    APP.run()

In streamlit: st_player("http://127.0.0.1:5000/"+ media["filepath"])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants