Skip to main content

Package media content for online ffmpeg_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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python-ffmpeg-video-streaming-0.0.1.tar.gz (7.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

python_ffmpeg_video_streaming-0.0.1-py3-none-any.whl (10.1 kB view details)

Uploaded Python 3

File details

Details for the file python-ffmpeg-video-streaming-0.0.1.tar.gz.

File metadata

  • Download URL: python-ffmpeg-video-streaming-0.0.1.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.18.4 setuptools/41.0.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

File hashes

Hashes for python-ffmpeg-video-streaming-0.0.1.tar.gz
Algorithm Hash digest
SHA256 f59a59eada9dbd5f0a1cecf495675e8968681a93ef19bb9b67d62520fb2d77eb
MD5 4bb9ce394818ec0a2201c6674cc4229f
BLAKE2b-256 2d03836ce6e441ab044120084bc85d02a06e726536d17cbda84ed4f8ecd44bb7

See more details on using hashes here.

File details

Details for the file python_ffmpeg_video_streaming-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: python_ffmpeg_video_streaming-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 10.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.18.4 setuptools/41.0.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

File hashes

Hashes for python_ffmpeg_video_streaming-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 24f88231aa82707fae49d79558c3e5b04884cfac8d3e970c52c58a8ef7e6912b
MD5 e692eb4a46716efd507f2970d07847fd
BLAKE2b-256 dfeaff74a01a04fab04090dadee74dfab25ba054e44a29e0b4ac77f21465e7bd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page