Skip to content

Instantly share code, notes, and snippets.

@tzmartin
Last active November 17, 2024 04:41
Show Gist options
  • Save tzmartin/fb1f4a8e95ef5fb79596bd4719671b5d to your computer and use it in GitHub Desktop.
Save tzmartin/fb1f4a8e95ef5fb79596bd4719671b5d to your computer and use it in GitHub Desktop.
m3u8 stream to mp4 using ffmpeg

1. Copy m3u8 link

Alt text

2. Run command

echo "Enter m3u8 link:";read link;echo "Enter output filename:";read filename;ffmpeg -i "$link" -bsf:a aac_adtstoasc -vcodec copy -c copy -crf 50 $filename.mp4

Alt text

Example final command

ffmpeg -i "http://host/folder/file.m3u8" -bsf:a aac_adtstoasc -vcodec copy -c copy -crf 50 file.mp4
@tronghieu60s
Copy link

I'm having a problem trying to decode when the player is using the fake img link, I see the video file has a ?PNG at the beginning of the content, anyone have a way to fix this?

File .m3u8

#EXT-X-VERSION:3
#EXT-X-TARGETDURATION:7
#EXT-X-PLAYLIST-TYPE:VOD
#EXTINF:6
https://s5.bestchill.workers.dev/sg/MjAyMjA5MjJfMjgg/MDAxNjYzODM5MTIyMjQz.2NlYD5uKQINWCu4tSseNg5wAEMPSpLqy1uPC7PsqwrAg.rPpnj1itZm2hbQWcxJ3uGzDHhzhWQDeirLrwZ4zrcHkg/index.html
#EXTINF:6
https://s1.bestchill.workers.dev/sg/MjAyMjA5MjJfMTM0/MDAxNjYzODM5MTIyMjA4.09L5NGYqCUWXn0Ff__jugCt3hzYxnovpjrvg0fG4SVIg.CXsKbhRODTttikxbAd0RQiQnvvWDAKtU_cNhlsieookg/index.html
#EXTINF:5.6
https://s3.bestchill.workers.dev/sg/MjAyMjA5MjJfNDgg/MDAxNjYzODM5MTM0OTc1.o-awTLVDuR2c1fV7bdavYODnsl7M0p5PCP05Q07fjiQg.8LIfxzp--TCKU3buOCkznGCGHMvnyabAbjvyH2pN0MEg/index.html
#EXTINF:6
...

File HLS index.html

�PNG � IHDR����ؿů pHYs�������� cHRMz%������u0�`:��o�_�F�IDATx�b,�����������
...

This is 2 file example:

https://github.com/tronghieu60s/tronghieu60s.github.io/blob/master/index.m3u8
https://github.com/tronghieu60s/tronghieu60s.github.io/blob/master/index.html

@ilyazub
Copy link

ilyazub commented Oct 3, 2022

@varenc Thank you the solution with yt-dlp and aria2c!

@luxin88
Copy link

luxin88 commented Jan 25, 2023

if the url in m3u8 should with some header(like cookie), how to run it?

sometime some url thow 403, if I ue curl with header, it run successful

maybe we can add a Reverse Proxy between ffmpeg and site

@raleighlittles
Copy link

If you run this exact command on newer FFMPEG versions you'll probably get an error about something not being on the whitelist: protocol https not on whitelist

Modify the command to use this instead

$ ffmpeg -protocol_whitelist file,https,tcp,tls -i master.m3u8 -bsf:a aac_adtstoasc -vcodec copy -c copy -crf 50 video.mp4 

Tested with FFMPEG versions 5.0 and newer

@ImArghya7
Copy link

Thanks! How to speed up the download (like, download concurrently?)

Use N_m3u8DL-RE with -mt argument.

@sanjukhunt
Copy link

Tried using VLC and it was being tempermental on me, this worked so much better thank you

can you please share more details how we can convert using VLC Kit?

@kolbdog3333
Copy link

Works great also i noticed if you copy a 1280x720 youtube video url from download helper and use this method it downloads the 1920x1080 video instead it downloads a higher qualtiy video then the original. It works perfectly thank you.

@garretttaco
Copy link

This works perfectly, thanks for sharing!

@geraldotech
Copy link

nice work

@akdev000
Copy link

You can use this approach to emulate the same headers that your browser sends, thereby bypassing 403 Forbidden errors:

Using Firefox, open Developer Tools using Ctrl + Shift + I, or click on the 3-line hamburger menu in the top-right (1.) > More Tools (2.) > Web Developer Tools (3.).

Firefox: 3-line Hamburger Menu > More Tools
Firefox: More Tools > Developer Tools

Go to the Network tab (4.), reload the page, find the appropriate .m3u8 entry, typically named stream.m3u8 or something similar (5.), right-click, Copy Value (6.) > Copy as cURL (7.).

stream.m3u8: Copy Value > Copy as cURL

Then paste the resulting cURL command into here: https://windyakin.github.io/curl2ffmpeg. Download and install the free, open source FFMpeg tool if you don't already have it, paste the resulting ffmpeg command into your commandline (you may have to add a .exe suffix, eg: ffmpeg becomes ffmpeg.exe if you are using Windows) and enjoy the video!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment