Skip to main content

Package media content for online streaming(DASH and HLS) using ffmpeg

Project description

📼 Python FFmpeg Video Streaming

Build Status Build status Downloads PyPI version Software License

This package uses the FFmpeg to package media content for online streaming(DASH and HLS)

Contents

Requirements

  1. This version of the package is only compatible with Python 3.6 or higher.

  2. To use this package, you need to install the FFMpeg. You will need both FFMpeg and FFProbe binaries to use it.

Installation

The latest version of ffmpeg-streaming can be acquired via pip:

pip install python-ffmpeg-video-streaming

Quickstart

The best way to learn how to use this library is to review the examples and browse the source code as it is self-documented.

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.

Similar to Apple's HTTP Live Streaming (HLS) solution, MPEG-DASH works by breaking the content into a sequence of small HTTP-based file segments, each segment containing a short interval of playback time of content that is potentially many hours in duration, such as a movie or the live broadcast of a sports event. The content is made available at a variety of different bit rates, i.e., alternative segments encoded at different bit rates covering aligned short intervals of playback time. While the content is being played back by an MPEG-DASH client, the client uses a bit rate adaptation (ABR) algorithm to automatically select the segment with the highest bit rate possible that can be downloaded in time for playback without causing stalls or re-buffering events in the playback. The current MPEG-DASH reference client dash.js offers both buffer-based (BOLA) and hybrid (DYNAMIC) bit rate adaptation algorithms. Thus, an MPEG-DASH client can seamlessly adapt to changing network conditions and provide high quality playback with fewer stalls or re-buffering events. Learn more

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')
)

You can also 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')
)

See DASH options for more information.

HLS

HTTP Live Streaming (also known as HLS) is an HTTP-based adaptive bitrate streaming communications protocol implemented by Apple Inc. as part of its QuickTime, Safari, OS X, and iOS software. Client implementations are also available in Microsoft Edge, Firefox and some versions of Google Chrome. Support is widespread in streaming media servers.

HLS resembles MPEG-DASH in that it works by breaking the overall stream into a sequence of small HTTP-based file downloads, each download loading one short chunk of an overall potentially unbounded transport stream. A list of available streams, encoded at different bit rates, is sent to the client using an extended M3U playlist. Learn more

Create HLS files based on original video(auto generate qualities).

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')
)

You can also 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')
)

NOTE: You cannot use HEVC(libx265) and VP9 formats for HLS packaging.

Encrypted 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 ciphertext of the preceding block. Learn more

You need to pass both URL to the key and path to save a random key to the encryption method:

import ffmpeg_streaming

(
    ffmpeg_streaming
        .hls('/var/www/media/videos/test.mp4', hls_time=10, hls_allow_cache=1)
        .encryption('https://www.aminyazdanpanah.com/keys/enc.key', '/var/www/my_website_project/keys/enc.key')
        .format('libx264')
        .auto_rep()
        .package('/var/www/media/videos/hls/test.m3u8')
)

NOTE: It is very important to protect your key on your website using a token or a session/cookie(It is highly recommended).

See HLS options for more information.

Progress

You can get realtime information about the transcoding by passing a callable method to the package method:

import ffmpeg_streaming

def progress(percentage, line, all_media):
    # You can update a field in your database
    # You can also create a socket connection and show a progress bar to users
    print("{}% is transcoded".format(percentage))

(
    ffmpeg_streaming
        .hls('/var/www/media/videos/test.mp4', hls_time=10, hls_allow_cache=1)
        .encryption('https://www.aminyazdanpanah.com/keys/enc.key', '/var/www/my_website_project/keys/enc.key')
        .format('libx264')
        .auto_rep()
        .package('/var/www/media/videos/hls/test.m3u8', progress)
)

Output:

1% is transcoded
2% is transcoded
.
.
.
99% is transcoded
100% is transcoded

Process finished with exit code 0

Probe

You can extract the metadata of video file using the following code:

from pprint import pprint
from ffmpeg_streaming import FFProbe

ffprobe = FFProbe('/var/www/media/test.mp4')

NOTE: You can save these metadata to your database.

See the example for more information.

Several Open Source Players

You can use these libraries to play your streams.

NOTE: You should pass a manifest of stream(e.g. https://www.aminyazdanpanah.com/videos/dash/lesson-1/test.mpd or /videos/hls/lesson-2/test.m3u8 ) to these players.

Contributing and Reporting Bugs

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.
  • If you have any questions or you want to report a bug, please just file an issue
  • If you discover a security vulnerability within this package, please see SECURITY File for more information to help with that.

NOTE: If you have any questions about this package or FFMpeg, please DO NOT send an email to me or submit the contact form on my website. Emails related to these issues will be ignored.

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.8.tar.gz (13.8 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.8-py3-none-any.whl (14.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: python-ffmpeg-video-streaming-0.0.8.tar.gz
  • Upload date:
  • Size: 13.8 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.8.tar.gz
Algorithm Hash digest
SHA256 7df4151800bbc7a16a276f67a4a77b861cd8e0e95ed142763b8419346ef92df8
MD5 4325e77816d85c216e40f01980991d04
BLAKE2b-256 78daa377474f7f6228c7b11bd79180b698f3087c214b6e05a0464a00d1c52f1e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: python_ffmpeg_video_streaming-0.0.8-py3-none-any.whl
  • Upload date:
  • Size: 14.6 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.8-py3-none-any.whl
Algorithm Hash digest
SHA256 04ef276ce2c27cf4ddfba55c81b7f7f951996958f52bfeff5fada4fed654c80c
MD5 68ce1aac26cf931b911b56fb7e70af53
BLAKE2b-256 77a1d9528ca1357df01f2d6b4c4306f4df39a9f7c366fa5daea9055f9b09dd72

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