streamlit学习-如何播放HLS视频(streamlit嵌入html)

streamlit学习-如何播放HLS视频

本文演示了streamlit如何实现hls直播[streamlit中嵌入html]

一.效果

二.直播环境搭建(仅供演示)

1.生成m3u8

bash 复制代码
ffmpeg.exe -i abc.mp4 -c:v libx264 -an -vbsf h264_mp4toannexb -f hls playlist.m3u8

2.搭建http服务器(支持跨域)

python 复制代码
import os
import sys
import http.server
import socketserver
class HTTPRequestHandler(http.server.SimpleHTTPRequestHandler):
    def end_headers(self):
        self.send_header('Access-Control-Allow-Origin', '*')
        self.send_header('Access-Control-Allow-Methods', 'GET,POST')
        self.send_header('Access-Control-Allow-Headers', 'x-requested-with,content-type')
        http.server.SimpleHTTPRequestHandler.end_headers(self)
def server(port):
    httpd = socketserver.TCPServer(('', port), HTTPRequestHandler)
    return httpd
srv=server(8000)
srv.serve_forever()

3.验证hls(VLC播放 http://localhost:8000/playlist.m3u8)

三.streamlit demo

python 复制代码
import streamlit as st  #1.31.1
import os
import sys
import time
import streamlit.components.v1 as components

def main():
    # 设置标题,图标等
    st.set_page_config(layout="centered",page_title="HLS播放",page_icon=":shark:")
    
    st.write("演示HLS播放")
    html_content='''
<html> 
<head>
    <meta charset="utf-8">
    <style>
        .video-js {
            width: 100%;
            height: 100%;
        }
    </style>
    <script src="https://unpkg.com/hls.js@0.14.17/dist/hls.js"></script>
    <body>
        <video id="videoPlayer" controls class="video-js"></video>
    </body>
    <script>
    var video = document.getElementById('videoPlayer');
    var hls = new Hls();
    if (Hls.isSupported()) {
      hls.loadSource('http://localhost:8000/playlist.m3u8');
      hls.attachMedia(video);
      hls.on(Hls.Events.MANIFEST_PARSED, function() {
        video.play();
      });
    }
    </script>
</html>
    '''
    components.html(html_content,height=400)
    st.button("按钮")
if __name__ == "__main__":
    main()
相关推荐
anOnion2 天前
构建无障碍组件之Radio group pattern
前端·html·交互设计
前端Hardy2 天前
HTML&CSS&JS:丝滑无卡顿的明暗主题切换
javascript·css·html
前端Hardy3 天前
HTML&CSS&JS:基于定位的实时天气卡片
javascript·css·html
前端Hardy3 天前
HTML&CSS:纯CSS实现随机转盘抽奖机——无JS,全靠现代CSS黑科技!
css·html
DeathGhost3 天前
分享URL地址到微信朋友圈没有缩略图?
前端·html
西岸行者4 天前
学习笔记:SKILLS 能帮助更好的vibe coding
笔记·学习
REDcker4 天前
WebCodecs VideoDecoder 的 hardwareAcceleration 使用
前端·音视频·实时音视频·直播·webcodecs·videodecoder
gihigo19984 天前
基于TCP协议实现视频采集与通信
网络协议·tcp/ip·音视频
悠哉悠哉愿意4 天前
【单片机学习笔记】串口、超声波、NE555的同时使用
笔记·单片机·学习
别催小唐敲代码4 天前
嵌入式学习路线
学习