29

Can anyone advise on how to construct an MP4 file from an HLS stream (the reverse of what you usually want)? Say I have the m3u8 - is there a straightforward way of getting a single MP4 using FFMPEG or some other tool?

orcaman
  • 6,263
  • 8
  • 54
  • 69

2 Answers2

60

ffmpeg -i in.m3u8 -acodec copy -vcodec copy out.mp4

For AAC audio you will also need to add the the bit ststream filter. (Thanks @aergistal for pointing that out)

ffmpeg -i in.m3u8 -acodec copy -bsf:a aac_adtstoasc -vcodec copy out.mp4

szatmary
  • 29,969
  • 8
  • 44
  • 57
  • 1
    Hi. Can you help me how to do this on swift? I want to download m3u8 file but can not save because it not support, now i want to convert to mp4 in swift – famfamfam Jan 20 '19 at 07:13
  • Sure, open a question on stackoverflow detailing exactly what step you have having problems with, and I’d be happy to help. – szatmary Jan 20 '19 at 07:15
  • Hi. Im really happy when read your comment, i posted the question here, can u take a look: https://stackoverflow.com/questions/54274457/swift-can-not-save-m3u8-file-to-gallery – famfamfam Jan 20 '19 at 07:45
  • Would this command generate an MP4 file with all the alternate audio tracks and side-car VTT captions from the HLS to burn in captions and audio tracks within the MP4? – Manik Arora Sep 13 '21 at 14:41
4

An alternative way of converting an HLS to MP4 is using VLC Player. You can do the conversion through the interface as well as commandline. Simply you can run a .bat file which has following lines:

chcp 65001
"C:\Program Files (x86)\VideoLAN\VLC\vlc.exe" http://cdn.haramain.global/vods3/_definst_/mp4:amazons3/akra/programlar/yeni/335/c07f21e3-a313-4e8d-b594-403ddefbf11f.mp4/playlist.m3u8 --sout "#transcode{vcodec=none,acodec=mp3,ab=70,channels=2,samplerate=44100}:std{access=file{no-overwrite},mux=mp3,dst='C:\Users\aidata\Desktop\Akra FM\13.07.2013 - Karagöz - Bilmecesi.mp3'}" vlc://quit
"C:\Program Files (x86)\VideoLAN\VLC\vlc.exe" http://cdn.haramain.global/vods3/_definst_/mp4:amazons3/akra/programlar/yeni/335/dcb6754a-49f1-4517-bfe4-3864942f63c8.mp4/playlist.m3u8 --sout "#transcode{vcodec=none,acodec=mp3,ab=70,channels=2,samplerate=44100}:std{access=file{no-overwrite},mux=mp3,dst='C:\Users\aidata\Desktop\Akra FM\12.07.2013 - Nasreddin Hoca - Köyün Eseği.mp3'}" vlc://quit

This batch script converts two files one after another. If you had pasted these commands into cmd.exe, all conversions would start at the same time.


Now let me explain the codes. The chcp 65001 line allows you to use unicode characters in the destination file name. Following lines consist of four parts.

  1. "C:\Program Files (x86)\VideoLAN\VLC\vlc.exe"

This is the path of the VLC player. Verify this after installing VLC player.

  1. http://cdn.haramain.global/vods3/_definst_/mp4:amazons3/akra/programlar/yeni/335/c07f21e3-a313-4e8d-b594-403ddefbf11f.mp4/playlist.m3u8

This is a sample HLS file. I don't know what happens if you put this link in double quotes.

  1. --sout "#transcode{vcodec=none,acodec=mp3,ab=70,channels=2,samplerate=44100}:std{access=file{no-overwrite},mux=mp3,dst='C:\Users\aidata\Desktop\Akra FM\13.07.2013 - Karagöz - Bilmecesi.mp3'}"

This is the VLC command for conversion. You can find more options in VLC Documentation

  1. vlc://quit

This will close the VLC window. It is helpful if you don't want your taskbar to be filled with VLC windows. There is no way to stack conversion orders in playlist. You have to run VLC, do the conversion and close the window. You can also try running VLC in silent mode. Or you can drag a VLC window to right bottom of your screen so that subsquent flashing windows won't disturb you.

sevenkul
  • 966
  • 1
  • 8
  • 14