ffmpeg concatenation output freezing up after a few minutes

Viewed 18

In a folder I have about 170 videos, each one second long. I have checked them and they are all fine. When I run an ffmpeg command to concatenate them, the output.mp4 file has some issues.

It all goes fine for about a minute. Then, the video stops changing. At the three-minute mark the audio stops (should be end of video), but the video continues, and seems to play the rest of the videos very slowly. I followed the advice of this question (https://video.stackexchange.com/questions/18247/ffmpeg-concat-demuxer-corrupted-output-freezed-video-on-some-concatenated-parts) and forced re-encoding. It made it slightly better, but still broken after 1min30s. Is there a better way to concatenate to minimize these corruptions?

The command I used is ffmpeg -f concat -safe 0 -i mylist.txt output.mp4

Windows 11 with FFMPEG 5.1.1

Trying on Ubuntu with an older ffmpeg from apt yielded even worse results with failures occuring from around the 40s mark even with forced re-encoding.

1 Answers

OK. I incorrectly assumed that every file was the same stream type. I was very incorrect. Half of them were at a different frame rate.

Since I am using Python for it, here is the code to extract one second and concat

import subprocess

import glob
import sys
import os
from awesome_progress_bar import ProgressBar
import random
_exv = True
if _exv:
    g = glob.glob("*.mp4")
    bar = ProgressBar(int(len(g)),prefix="Extracting",suffix="Preparing",bar_length=200,spinner_type="s")
    for v in g:
        bar.iter()
        _len = int(get_length(v) // 1)# Rounding down
        if _len > 2:
            slt = random.randint(0,_len)
            bar.suffix = str(v)
            rmn = subprocess.run(["ffmpeg","-ss",str(slt),"-i",os.getcwd()+"/"+v,"-t","1",os.getcwd()+"/"+v.split(".")[0]+"s.mp4"],stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
            rmn2 = subprocess.run(["ffmpeg","-i",os.getcwd()+"/"+v.split(".")[0]+"s.mp4","-filter_complex","[0:v]pad=1280:720:-1:-1,fps=30000/1001[v];[0:a]aformat=sample_rates=44100:channel_layouts=stereo[a]","-map","[a]","-map","[v]",os.getcwd()+"/"+v.split(".")[0]+"x.mp4"],stdout=subprocess.PIPE,stderr=subprocess.STDOUT)

    bar.wait()
    bar.stop()

lg = glob.glob("*x.mp4")#Filtering split videos only
data = ""
for vid in lg:
    data += "file '"
    data += os.getcwd()
    data += "/"
    data += vid
    data += "'\n"
with open("mylist.txt","w+") as f:
    f.write(data)

rmn2 = subprocess.run(["ffmpeg","-f","concat","-safe","0","-i","mylist.txt","output.mp4"],stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
Related