Python Ffmpeg Download M3u8 ((TOP))

0 views
Skip to first unread message

Lexie Rangitsch

unread,
Jan 21, 2024, 6:02:13 AM1/21/24
to fonlavine

I have a HLS stream and wanted to extract frame as it appears for computer vision using opencv in python. I have tried exploring ffmpeg but it seems to read .mp4 easily but not the hls stream (m3u8). Is there any other option or other API of ffmpeg-python to extract frame from HLS stream.

python ffmpeg download m3u8


Download Filehttps://t.co/9pcKr4Hlp6



The latest version of ffmpeg pulled from git (tested with ffmpeg version git-2013-08-15-165b657) will generate the segment playlist file in m3u8 format if you specify the m3u8 extension for your output file when using the -segment_list argument.

where the generate the filelist.txt from the index.m3u8 file, it complains about not being able to read various files. But converting the ts files themselves seem to work fine. I think I'm not using ffmpeg properly.

All the popular answers to this question that mislead readers to concatenate the TS files before running ffmpeg are incorrect. To ensure the audio and video do not fall out of sync during the assembly of the mp4 stream, the poorly documented but important "-f concat" feature of ffmpeg should be used.

I downloaded video file xxxx-ad .m3u8 recently and I tried to solved the same problem (downloaded with Apowersoft from online stream). The file was only 201b big and it needed internet connection to play.

At least in recent versions of Internet Download Manager, you should get a .ts file rather than .m3u8 (unless you copy an .m3u8 link and try adding that as a download manually, as it supports downloading HLS (.m3u8) streams properly. You can play that file directly in some players. If you want an .mp4 file, you can use a tool like MyMP4BoxGUI, or FFmpeg:

Rename m3u8 to .txt, look at the filename/path the playlist points to. Find the file on your system, download it to your system if it's a network. Open this file in iTunes, iCloud and transfer the file to iPhone using iTunes/iCloud.

Changing the extension worked for me.
I downloaded a movie using internet download manager in .m3u8 format changed the extension to .mp4
POOFcomputer science at work - I was able to play the file.

The exact semantics of stream specifiers is defined by theavformat_match_stream_specifier() function declared in thelibavformat/avformat.h header and documented in the(ffmpeg)Stream specifiers section in the ffmpeg(1) manual.

This option is enabled by default. Disabling the channel mask can be useful inspecific scenarios, e.g. when merging multiple audio streams into one forcompatibility with software that only supports a single audio stream in AVI(see (ffmpeg-filters)the "amerge" section in the ffmpeg-filters manual).

You can select the output format of each frame with ffmpeg byspecifying the audio and video codec and format. For example tocompute the CRC of the input audio converted to PCM unsigned 8-bitand the input video converted to MPEG-2 video, use the command:

With ffmpeg, you can select the output format to which theaudio and video frames are encoded before computing the CRC for eachpacket by specifying the audio and video codec. For example, tocompute the CRC of each decoded input audio frame converted to PCMunsigned 8-bit and of each decoded input video frame converted toMPEG-2 video, use the command:

filename may contain full path or relative path specification,but only the file name part without any path info will be contained in the m3u8 segment list.Should a relative path be specified, the path of the created segmentfiles will be relative to the current working directory.When strftime_mkdir is set, the whole expanded value of filename will be written into the m3u8 segment list.

This example will produce the playlist, out.m3u8, and segment files:file-20160215-1455569023.ts, file-20160215-1455569024.ts, etc.Note: On some systems/environments, the %s specifier is not available. See strftime() documentation.

This example will create a directory 201560215 (if it does not exist), and thenproduce the playlist, out.m3u8, and segment files:20160215/file-20160215-1455569023.ts, 20160215/file-20160215-1455569024.ts, etc.

This example will create a directory hierarchy 2016/02/15 (if any of them do not exist), and thenproduce the playlist, out.m3u8, and segment files:2016/02/15/file-20160215-1455569023.ts, 2016/02/15/file-20160215-1455569024.ts, etc.

Round the duration info in the playlist file segment info to integervalues, instead of using floating point.If there are no other features requiring higher HLS versions be used,then this will allow ffmpeg to output a HLS version 2 m3u8.

Write segment data to filename.tmp and rename to filename only once the segment is complete. A webserverserving up segments can be configured to reject requests to *.tmp to prevent access to in-progress segmentsbefore they have been added to the m3u8 playlist. This flag also affects how m3u8 playlist files are created.If this flag is set, all playlist files will written into temporary file and renamed after they are complete, similarly as segments are handled.But playlists with file protocol and with type (hls_playlist_type) other than vodare always written into temporary file regardless of this flag. Master playlist files (master_pl_name), if any, with file protocol,are always written into temporary file regardless of this flag if master_pl_publish_rate value is other than zero.

This example will upload all the mpegts segment files to the HTTPserver using the HTTP PUT method, and update the m3u8 files everyrefresh times using the same method.Note that the HTTP server must support the given method for uploadingfiles.

This example creates two hls variant streams. The first variant stream willcontain video stream of bitrate 1000k and audio stream of bitrate 64k and thesecond variant stream will contain video stream of bitrate 256k and audiostream of bitrate 32k. Here, two media playlist with file names out_0.m3u8 andout_1.m3u8 will be created. If you want something meaningful text instead of indexesin result names, you may specify names for each or some of the variantsas in the following example.

This example creates three hls variant streams. The first variant stream willbe a video only stream with video bitrate 1000k, the second variant stream willbe an audio only stream with bitrate 64k and the third variant stream will be avideo only stream with bitrate 256k. Here, three media playlist with file namesout_0.m3u8, out_1.m3u8 and out_2.m3u8 will be created.

Note that with ffmpeg, if the format is not specified with the-f option and the output filename specifies an image fileformat, the image2 muxer is automatically selected, so the previouscommand can be written as:

Allow the caller to manually choose when to cut fragments, bycalling av_write_frame(ctx, NULL) to write a fragment withthe packets written so far. (This is only useful with otherapplications integrating libavformat, not from ffmpeg.)

Set PAT, PMT, SDT and NIT version (default 0, valid values are from 0 to 31, inclusively).This option allows updating stream structure so that standard consumer maydetect the change. To do so, reopen output AVFormatContext (in case of APIusage) or restart ffmpeg instance, cyclically changingtables_version value:

In particular may be used in combination with the ffmpeg optionforce_key_frames. The key frame times specified byforce_key_frames may not be set accurately because of roundingissues, with the consequence that a key frame time may result set justbefore the specified time. For constant frame rate videos a value of1/(2*frame_rate) should address the worst case mismatch betweenthe specified time and the time set by force_key_frames.

It is different from specifying several outputs to the ffmpegcommand-line tool. With the tee muxer, the audio and video data will be encoded only once.With conventional multiple outputs, multiple encoding operations in parallel are initiated,which can be a very expensive process. The tee muxer is not useful when using the libavformat APIdirectly because it is then possible to feed the same packets to several muxers directly.

What I want to achieve is to host the script with Colab so the process can be fast and be automate. I don't want to run this from my local environment anymore cause it uses a lot of pc resources and data(to download, process with ffmpeg and re-upload to gdrive)

Thanks @Chudsaviet [Chudsaviet]. Good to know about the hls_time setting. Will have a play. As some side testing I did manage to use ffmpeg and GitHub - aler9/rtsp-simple-server: ready-to-use RTSP / RTMP / HLS server and proxy that allows to read, pu to stream via HLS as well but I havent managed to tweak the latency yet. Approach is the same so naturally there will be lag.

Yep, I'm seeing 30s latency. I've seen some info on the internet that suggests there exists code that can configure the bitrate and gop/keyframe interval on these cameras, but I don't think that code exists in ffmpeg. I'll probably just stick with the long latency, or try out 1080p with h264_omx encoder (like in the default ffmpeg_hls.service unit).

The three files file_1.ts, file_2.ts, and file_3.ts have relative paths in this example, so would be expected to be downloaded from the same path the (or in the same folder) as the .m3u8 file. You will see later how we can change that to suit our need.

In this loop we will be adding a new entry in the global encrypted dictionary that is a list which contains more dictionaries that are in the "key": key, "iv": iv format. The key is in bytes, but the iv is a string that is hexideciamal. They could both be bytes or both be hex, just went with this way because we immediately use the IV as a hex string in the m3u8 whereas we always need the key in raw byes.

later, if I add another stream, say 720p. I want my master.m3u8 to include that stream and serve the clients. How to achieve this? Are there any existing commands in FFmpeg that I can use here? or create the master.m3u8 programmatically?

So a quick bit of history. I am using tvheadend, and the inbuilt transcoder murders my CPU, and provides average quality output (it's powered by ffmpeg), if I pipe the output from tvheadend through ffmpeg I get much lower CPU usage and better quality (weird but true). (I am having to transcode because of limited player abilities)

df19127ead
Reply all
Reply to author
Forward
0 new messages