brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: ffmpeg -n refuses to overwrite a file that already exists

Ffmpeg
~/tips/tip-ffmpeg-n-no-overwrite

ffmpeg -n -i "$f" -c copy "${f%.mkv}.mp4"

Makes ffmpeg skip an output that is already there instead of asking or replacing it.

ffmpeg asks before it replaces an existing output file. In a script there is nobody to answer, so you have to choose: -y to overwrite or -n to never overwrite.

for f in *.mkv; do
  ffmpeg -n -i "$f" -c copy "${f%.mkv}.mp4"
done

With -n, ffmpeg checks the output name first. If the file is there, it prints “File ‘name’ already exists. Exiting.” and moves on. That makes a batch job safe to run again after it was interrupted: finished files are left alone and only the missing ones are made.

I prefer -n over -y for anything that writes to a folder with originals in it. A typo in an output name with -y can replace a file you wanted to keep, without a single question.

There’s a gap though. A half-written file from an interrupted run also counts as “exists”, so -n skips it and you keep the broken one. Delete the last file the loop was working on before you start again, or write to a temporary name and rename it when ffmpeg is done. Also check your script logic separately: ffmpeg 9 exits with status 0 when it skips, so you can’t use the exit code to tell “skipped” from “done”.

Further reading: ffmpeg documentation

More Ffmpeg tips ffmpeg -stream_loop 2 -i in.mp4 -c copy out.mp4 ffmpeg -i in.mp4 \ ffmpeg -i video.mp4 -i video.en.srt -map 0 -map 1 -c copy -c:s mov_text -metadata:s:s:0 language=eng video-subs.mp4 ffmpeg -i film.mkv -map 0:v -map 0:a:0 -c:v copy -c:a aac -b:a 192k -ac 2 film-stereo.mkv ffmpeg -i talk.mp4 -vn -c:a copy talk.m4a All Ffmpeg tips (38) All topics

Link