使用ffmpeg为在线流媒体(短线和hls)打包媒体内容
python-ffmpeg-video-streaming的Python项目详细描述
python ffmpeg视频流
此库使用FFmpeg打包用于在线流媒体的媒体内容(短划线和HLS)
安装
最新版本的ffmpeg-streaming
可以通过pip获得:
pip install python-ffmpeg-video-streaming
文件
学习如何使用这个库的最好方法是查看示例并浏览自文档化的源代码。
所需库
这个库需要一个工作的FFMpeg和FFProbe二进制文件来使用它
对于安装ffmpeg和ffprobe,只需google“install ffmpeg on”+your operation system
破折号
Dynamic Adaptive Streaming over HTTP (DASH),也称为MPEG-DASH,是一种自适应比特率流技术,能够通过Internet通过传统的HTTP web服务器传输高质量的媒体内容流
自动创建短划线文件
importffmpeg_streaming(ffmpeg_streaming.dash('/var/www/media/videos/test.mp4',adaption='"id=0,streams=v id=1,streams=a"').format('libx265').auto_rep().package('/var/www/media/videos/dash/test.mpd'))
手动创建表示
importffmpeg_streamingfromffmpeg_streamingimportRepresentationrep1=Representation(width=256,height=144,kilo_bitrate=200)rep2=Representation(width=426,height=240,kilo_bitrate=500)rep3=Representation(width=640,height=360,kilo_bitrate=1000)(ffmpeg_streaming.dash('/var/www/media/videos/test.mp4',adaption='"id=0,streams=v id=1,streams=a"').format('libx265').add_rep(rep1,rep2,rep3).package('/var/www/media/videos/dash/test.mpd'))
importffmpeg_streamingfromffmpeg_streamingimportRepresentationrep1=Representation(width=256,height=144,kilo_bitrate=200)rep2=Representation(width=426,height=240,kilo_bitrate=500)rep3=Representation(width=640,height=360,kilo_bitrate=1000)(ffmpeg_streaming.dash('/var/www/media/videos/test.mp4',adaption='"id=0,streams=v id=1,streams=a"').format('libx265').add_rep(rep1,rep2,rep3).package('/var/www/media/videos/dash/test.mpd'))
有关FFMpeg及其短划线选项的详细信息,see here。
HLS
HTTP Live Streaming (also known as HLS)是由Apple Inc实现的基于http的媒体流通信协议。
importffmpeg_streaming(ffmpeg_streaming.hls('/var/www/media/videos/test.mp4',hls_time=10,hls_allow_cache=1).format('libx264').auto_rep().package('/var/www/media/videos/hls/test.m3u8'))
手动创建表示
importffmpeg_streamingfromffmpeg_streamingimportRepresentationrep1=Representation(width=256,height=144,kilo_bitrate=200)rep2=Representation(width=426,height=240,kilo_bitrate=500)rep3=Representation(width=640,height=360,kilo_bitrate=1000)(ffmpeg_streaming.hls('/var/www/media/videos/test.mp4',hls_time=10,hls_allow_cache=1).format('libx264').add_rep(rep1,rep2,rep3).package('/var/www/media/videos/hls/test.m3u8'))
有关应传递给这些方法以及hls选项的值的详细信息,see here。
加密hls
加密过程需要某种密钥和加密算法
HLS在密码块链(CBC)模式下使用AES这意味着每个块都是使用前一个块的密码文本加密的。read more
在加密视频之前,我们需要一个加密密钥。我将使用openssl创建密钥,我们可以这样做:
openssl rand 16 > enc.key
下一步是生成一个IV。这个步骤是可选的(如果未提供值,则将使用段序列号。)
openssl rand -hex 16
ecd0d06eaf884d8226c33928e87efa33
当你很快需要它时,记下它的输出
要加密视频,我们需要告诉ffmpeg要使用什么加密密钥,密钥的URI,等等我们使用setHlsKeyInfoFile
方法并传递密钥信息文件的位置文件必须采用以下格式:
Key URI Path to key file IV (optional)
第一行指定将写入播放列表的密钥的uri。第二行是包含加密密钥的文件的路径,第三行(可选)包含初始化向量下面是一个例子(enc.keyinfo):
https://example.com/enc.key enc.key ecd0d06eaf884d8226c33928e87efa33
现在我们已经拥有了所需的一切,请运行以下代码来加密视频片段:
importffmpeg_streaming(ffmpeg_streaming.hls('/var/www/media/videos/test.mp4',hls_time=10,hls_allow_cache=1,hls_key_info_file="/path/to/keyinfo").format('libx264').auto_rep().package('/var/www/media/videos/hls/test.m3u8'))
贡献
我希望你能帮助改进,修正,增加规格。 请file an issue 或者submit a pull request
有关详细信息,请参见Contributing File。
安全性
如果您发现此软件包中存在安全漏洞,请通过以下途径向Amin Yazdanpanah发送电子邮件:
联系aminyazdanpanah–com
学分
许可证
麻省理工学院的执照。有关详细信息,请参见License File