LeDerp
LeDerp

Reputation: 623

Download HLS ( HTTP ) Stream video using python

I need to download a streaming video(from a URL) using python the command line argument would be:

ffmpeg -i URL stream.mp4

I know I can use the subprocess command

subprocess.call('ffmpeg -i '+ URL +' stream.mp4', shell=True)

Is there any alternative like a API that I can use instead of using subprocess command

Upvotes: 2

Views: 9540

Answers (2)

Mohammad Moallemi
Mohammad Moallemi

Reputation: 658

You can use python-ffmpeg-video-streaming, I've checked out its documentation and repository and it's a pretty neat project, from HLS ABR support to AWS S3 upload.

For HLS output follow these steps:

Step 1:
Install it using pip: pip install python-ffmpeg-video-streaming

Step 2:
Attaching a video source:

import ffmpeg_streaming


video = ffmpeg_streaming.input(VIEDO_URL)

Step 2:
Setting up video bitrates and output:

from ffmpeg_streaming import Formats, Bitrate, Representation, Size


_360p  = Representation(Size(640, 360), Bitrate(276 * 1024, 128 * 1024))
_480p  = Representation(Size(854, 480), Bitrate(750 * 1024, 192 * 1024))
_720p  = Representation(Size(1280, 720), Bitrate(2048 * 1024, 320 * 1024))

hls = video.hls(Formats.h264())
hls.representations(_360p, _480p, _720p)
hls.output('/var/media/hls.m3u8')

As I said earlier you can also upload the video segments to any S3 compatible cloud storage.

from ffmpeg_streaming import  S3, CloudManager


s3 = S3(aws_access_key_id='YOUR_KEY_ID', aws_secret_access_key='YOUR_KEY_SECRET', region_name='YOUR_REGION')
save_to_s3 = CloudManager().add(s3, bucket_name="bucket-name")

hls.output(clouds=save_to_s3)

For more information checkout the package's official documents

Happy transcoding!

Upvotes: -1

Doctor Frost
Doctor Frost

Reputation: 11

Here's an API to use ffmpeg in python.

http://mhaller.github.io/pyffmpeg/

Upvotes: 0

Related Questions