2

I have loads of videos named different things and different extensions (some mp4 some wmv etc).

I would like to run the command below but on every video in a specific directory and then save that in another directory, no overwrites of original files.

Some videos maybe duplicates, if that happens it should pause the script. For example there maybe video1.mp4 & video1.avi both can't be video1.mp4 in /home/videos/processedso the script should pause.

ffmpeg -i '/home/videos/unprocessed/vidabc.mp4' -f mp4 -vcodec libx264 -preset fast -profile:v main -acodec aac -movflags +faststart '/home/videos/processed/vidabc.mp4'
ffmpeg -i '/home/videos/unprocessed/vidxyz.mp4' -f mp4 -vcodec libx264 -preset fast -profile:v main -acodec aac -movflags +faststart '/home/videos/processed/vidxyz.mp4'
2
  • To clarify, it sounds like you have 2 questions: 1, how to run the command on all files in a directory, and 2, how to script a pause (maybe a confirm dialog?) in the event of a duplicate filename. Is that correct?
    – cherdt
    Jun 30, 2018 at 1:25
  • ffmpeg by default asks to replace so I think it may work fine without. Just wanted to make that clear if it would cause any problems
    – masterq
    Jun 30, 2018 at 2:01

1 Answer 1

2

You can process every file in a directory using the find command with the -execdir flag.

Example:

find /home/videos/unprocessed -type f \
   -execdir ffmpeg -i '{}' -f mp4 -vcodec libx264 -preset fast \
   -profile:v main -acodec aac -movflags +faststart '/home/videos/processed/{}.mp4' \;

-type f indicates that you want to find only files, not directories.

After the -execdir flag, '{}' is replaced with the path of the file, e.g. ./vidabc.mp4.

At the end of the -execdir command, include a \; to terminate the command.

Note that in this example, your resulting filenames might look like:

  • /home/videos/processed/vidabc.mp4.mp4
  • /home/videos/processed/vidxyz.avi.mp4
  • /home/videos/processed/vidxyz.mp4.mp4

If that is a problem, there are ways to address that. See Command “find” -exec replacing string for a similar example.

2
  • find: missing argument to `-execdir' Try 'find --help' for more information. when running find /home/vids/todo/new -type f \ -execdir ffmpeg -i '{}' -f mp4 -vcodec libx264 -preset fast \ -profile:v main -acodec aac -movflags +faststart '/home/vids/todo/newencoded/{}.mp4'
    – masterq
    Jun 30, 2018 at 17:45
  • 1
    I was missing the terminator to the command specified in -execdir. I've edited the post and added it. See the man page for find, particularly the sections for -exec and -execdir, for details.
    – cherdt
    Jul 1, 2018 at 17:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .