DEV Community

ricco020
ricco020

Posted on

Joining two-second clips into a film of the day with ffmpeg (and why -c copy alone breaks)

The idea: film two seconds, a few times a day, and in the evening get one short film of the whole day. With clips already on disk, ffmpeg can do it in two steps. The catch is that the obvious one-liner breaks as soon as the clips come from different phones.

The naive version

printf "file '%s'\n" *.mp4 > list.txt
ffmpeg -f concat -safe 0 -i list.txt -c copy film.mp4
Enter fullscreen mode Exit fullscreen mode

The concat demuxer copies streams as they are. If one clip is 1080p at 30 fps and the next is 4K at 60 fps, or one has no audio track, the result plays with frozen frames, a jump in size, or sound drifting away from the picture.

Normalise first, then join

Cut and re-encode each clip to the same shape, then concat with -c copy:

ffmpeg -t 2 -i "$f" \
  -f lavfi -t 2 -i anullsrc=channel_layout=stereo:sample_rate=44100 \
  -filter_complex "[0:v]scale=1080:1920:force_original_aspect_ratio=increase,crop=1080:1920,fps=30,format=yuv420p,setsar=1[v]" \
  -map "[v]" -map 1:a \
  -c:v libx264 -preset veryfast -crf 20 -c:a aac -ar 44100 -ac 2 -shortest part.mp4
Enter fullscreen mode Exit fullscreen mode

What each piece is for:

  • -t 2 before -i keeps the first two seconds of the input.
  • scale=...:force_original_aspect_ratio=increase,crop=1080:1920 fills a portrait frame and centre-crops, so a landscape clip does not get letterboxed.
  • fps=30,format=yuv420p,setsar=1 gives every part the same frame rate, pixel format and pixel aspect ratio. These three are the usual reasons a -c copy concat misbehaves.
  • anullsrc supplies a silent stereo track, so a clip recorded without sound still has an audio stream of the same layout as the others.

Order by recording time

A film of the day should follow the day. Sorting by modification time is enough when the files come straight from the phone:

find "$dir" -maxdepth 1 -type f -iname '*.mp4' -printf '%T@ %p\0' | sort -z -n
Enter fullscreen mode Exit fullscreen mode

The null separators keep file names with spaces intact.

The script

I wrapped this in a 40-line bash script, dayfilm.sh, that handles mp4, mov, m4v, 3gp and webm, keeps the clip's own sound when it has some, and falls back to silence when it does not:

./dayfilm.sh ~/Videos/today film.mp4
Enter fullscreen mode Exit fullscreen mode

I tested it on three generated clips of different shapes (640 x 360 at 25 fps without sound, 720 x 1280 at 30 fps, 1920 x 1080 at 60 fps with 48 kHz audio, the last one only one second long). The output is a single 1080 x 1920, 30 fps file with a stereo 44.1 kHz track, 5 seconds long.

Doing it with friends

The script is a one-person, one-folder version of what the app Klapday does for a closed group of friends: everyone films two seconds a few times a day and the app builds one shared film in the evening. I work on that app, and the normalising problem above is the same one at a larger scale, since every phone records differently.

Top comments (0)