How can I automatically create uniquely named video files with FFmpeg?
Asked by
6x7 negative
I'm working on a script for screen recording using FFmpeg, and I need a way to automatically name the output files incrementally (e.g., out1.mp4, out2.mp4, etc.), while ensuring that any existing files are skipped. I've thought of a few methods but they all seem overly complicated. Does anyone have a simpler solution?
Answers
Instead of using loops, you could simply use a timestamp in your output filename. For example, try this command: `ffmpeg -i [input] "out_$(date +%Y%m%d_%H%M%S).mp4"`. It will give you a unique name every time you record!
Reply to this answer
This method you shared is pretty solidโshort and straightforward! Just to confirm, this will indeed avoid writing over existing files? Iโm just making sure because thatโs been my main concern!
melted crayon: Yes, it will skip existing files. That's the beauty of using this loop! You won't overwrite anything.
Reply to this answer
You could also use a while loop to create your files incrementally. Something like: `i=1; while [[ -f out$i.mp4 ]]; do i=$((i + 1)); done; ffmpeg ... "out$i.mp4"` to ensure you donโt overwrite any existing files.
one.more.chapter: Thanks! I assume this will skip any existing files? Like if out1.mp4 is there, it would go to out2.mp4 instead? I'm still a bit new to this.
Reply to this answer
Using epoch time is another straightforward option: `out_$(date +%s)`. This way, you'll never have a filename collision.
Reply to this answer
Add your answer
Answers are reviewed before publication.
weld+6mm: This is a great method! I recommend using `+%F` instead of `+%Y%m%d` to make it easier to type, plus you could use `+%T` if you want the time too!
honeycomb structure: I hadn't considered that! Thanks for the suggestion. It sounds much simpler.