Package media content for online streaming(DASH and HLS) using ffmpeg
Project description
Python FFMpeg Video Streaming
This library uses FFmpeg to package media content for online streaming(DASH and HLS)
Installation
The latest version of ffmpeg-streaming
can be acquired via pip:
pip install python-ffmpeg-video-streaming
Documentation
It is recommended to browse the source code as it is self-documented.
Required Libraries
This library requires a working FFMpeg install. You will need both FFMpeg and FFProbe binaries to use it.
For installing FFmpeg and FFprobe, just Google "install ffmpeg on" + your operation system
DASH
Dynamic Adaptive Streaming over HTTP (DASH), also known as MPEG-DASH, is an adaptive bitrate streaming technique that enables high quality streaming of media content over the Internet delivered from conventional HTTP web servers.
Auto Create DASH Files
import ffmpeg_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')
)
Create Representations Manually
import ffmpeg_streaming
from ffmpeg_streaming import Representation
rep1 = 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')
)
For more information about FFMpeg and its dash options please click here.
HLS
HTTP Live Streaming (also known as HLS) is an HTTP-based media streaming communications protocol implemented by Apple Inc.
import ffmpeg_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')
)
Create Representations Manually
import ffmpeg_streaming
from ffmpeg_streaming import Representation
rep1 = 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')
)
For more information about which value you should pass to these methods and also HLS options please click here.
Encryption HLS
The encryption process requires some kind of secret (key) together with an encryption algorithm.
HLS uses AES in cipher block chaining (CBC) mode. This means each block is encrypted using the cipher text of the preceding block. read more
Before we can encrypt our videos, we need an encryption key. I’m going to use OpenSSL to create the key, which we can do like so:
openssl rand 16 > enc.key
The next step is to generate an IV. This step is optional. (If no value is provided, the segment sequence number will be used instead.)
openssl rand -hex 16
ecd0d06eaf884d8226c33928e87efa33
Make a note of the output as you’ll need it shortly.
To encrypt the video we need to tell ffmpeg what encryption key to use, the URI of the key, and so on. We use setHlsKeyInfoFile
method and passing the location of a key info file. The file must be in the following format:
Key URI
Path to key file
IV (optional)
The first line specifies the URI of the key, which will be written to the playlist. The second line is the path to the file containing the encryption key, and the (optional) third line contains the initialisation vector. Here’s an example (enc.keyinfo):
https://example.com/enc.key
enc.key
ecd0d06eaf884d8226c33928e87efa33
Now that we have everything we need, run the following code to encrypt the video segments:
import ffmpeg_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')
)
Reference: http://hlsbook.net/
Contributing
I'd love your help in improving, correcting, adding to the specification. Please file an issue or submit a pull request.
Please see Contributing File for more information.
Security
If you discover a security vulnerability within this package, please send an e-mail to Amin Yazdanpanah via: contact [AT] aminyazdanpanah • com.
Credits
License
The MIT License (MIT). Please see License File for more information.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for python-ffmpeg-video-streaming-0.0.2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9eadc2556dad8b28ab50f0801e9716d6486062f775ded95a0d7dc76109064119 |
|
MD5 | bb988ea8babb7d60ce97065b54647b7e |
|
BLAKE2b-256 | 2da3e5fda4eec720511d3f2f5173ba3513497a5cfce14c8b73480ca7a4aa7673 |
Hashes for python_ffmpeg_video_streaming-0.0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4cfd9a44483ef26bfe8e65d403a59bb0652ded85f49ce51446ae141a5a34dbd6 |
|
MD5 | 170ac2f13bc6d42e2ec307d60422260d |
|
BLAKE2b-256 | 256e7c18a0b7e29dbf2df18e485dbb57d0dea1f78533c504582b562e80e3cb45 |