[FFmpeg-user] Pasting together different parts of a downloaded video

Bo Berglund bo.berglund at gmail.com
Wed May 4 10:27:01 EEST 2022

This is for an Ubuntu 20.04 server.

I have a script that downloads a video live stream into a mp4 file while on the
fly changing the format, basically re-encoding the video.
It uses this basic download command:

CMD="ffmpeg -hide_banner ${MODE} -i \"${M3U8URL}\" -vf scale=w=-4:h=480 -c:v
libx264 -preset fast -crf 26 -c:a copy -t $DIFF $TARGETFILE"

Where the arguments are:
VIDEOURL=<webpage sourcing the stream>
MODE="-referer \"${VIDEOURL}\""
M3U8URL=<m3u8 url of actual stream>
DIFF=<number of seconds to record>
TARGETFILE=<output file name>

Sometimes when I download the stream is interrupted and ffmpeg exits with a
short video.
So I have added a ceck for this in the script and if the recorded time is less
than the requested time it waits for 60 s and then tries to download a second
part using the same command but different DIFf and TARGETFILE values.

If this results in a valid second file the script concatenates the two sections
into one using the following procedure:
- Create a joinfile with the two files listed
- Run the following ffmpeg command:

ffmpeg -f concat -safe 0 -i $JOINFILE -c copy $TARGETFILE

I had expected the concatenated file to be a combination of the two sections
with no real problems but it turns out that the audio is completely out of sync
in the output. Looks like by a *LOT*.

If I instead use another command like this to concatenate the two parts it works
fine, but takes a really long time, like 15 minutes:

ffmpeg -hide_banner -i input1.mp4 -i input2.mp4 -filter_complex "[0:v] [0:a]
[1:v] [1:a] concat=n=2:v=1:a=1 [v] [a]" -map "[v]" -map "[a]" output.mp4

Why does the original concat not work when the parts have been created using the
exact same command on the same video stream?

I expected this to be a no-issue operation since I have used it to edit video
files by first cutting a video into the relevant arts and the concatenating
these into an output. No issues then...

Grateful for any suggestions on what I can do!

Bo Berglund
Developer in Sweden

More information about the ffmpeg-user mailing list