Why am I having to sync between video and audio in the downloaded HLS video?

I wrote a script to download chunks (.ts) from a web page that streams some soccer videos and combines all the chunks in one file. The issue is that the final result is a video “lagging”, it seems that some frames are missing. I have used FFmpeg for combining the .ts chunks.

1 Like

You are doing it the hard way. The easy way is to let FFmpeg download everything and combine it nicely which does not cause any frame drop or sync issue. Whatever you did in your bash script can be done using a single FFmpeg command.

First, you have to check the stream indices of the streams you want to download using the ffprobe command because the HLS stream contains multiple video and audio streams.

Once you have the stream indices, you can use the following command to download the video.
ffmpeg -i hls_url -map 0:0 map 0:1 -c copy -f mp4 output.mp4

In the above command,

  • The input HLS URL is specified with -i hls_url. The streams will be downloaded by FFmpeg from this source.

  • -map 0:0: Assigns the video stream to the input stream that is the first (index 0). In this scenario, 0:0 corresponds to the first stream of the input, which is usually the video stream. The format for -map is -map input_index:stream_index.

  • -map 0:1 designates the audio stream as the second input stream (index 1). This is also expressed as 0:1, just like the video stream.

  • The stream copy option, -c copy, duplicates the streams without re-encoding them. This prevents needless loss of quality and is efficient. Stream copying is a wise decision because HLS streams frequently include content that has already been encoded.

  • -f mp4: Indicates the desired output format, which is MP4. By doing this, the MP4 format of the output file is guaranteed.

  • output.mp4: Indicates the name of the output file.

Let me know if you have further questions on this.

Great! Any suggestions on how to add a request header while downloading an HLS video using FFmpeg?

Same on my side, I want to download an HLS video from a URL that requires cookies and referer headers for accessing it. I
s there any way to add this header to FFmpeg?

Hey,

FFmpeg provides multiple options for specifying request headers. You can find a list of them at the following link.
https://ffmpeg.org/ffmpeg-all.html#http
For your use case, you have to -cookies and -referer options with your FFmpeg command like following
ffmpeg -cookies “xyz” -referer “https://xyz.com” -i hls_url -map 0:0 map 0:1 -c copy -f mp4 output.mp4
ffmpeg -cookies “xyz” -referer “https://xyz.com” -i hls_url -map 0:0 map 0:1 -c copy -f mp4 output.mp4I hope now this resolve your problem.

Thanks