How do i transcode all files in a directory with ffmpeg

9,425
for file in *.avi; do ffmpeg -i "$file" "${file%.avi}".webm; done

This is a Bash "for loop" and is great for repeating tasks. Section by section:

for file in *.avi;

file will become the variable in the next section that will refer to each particular avi in the current directory for every successive loop.

What to customize:

  • file could be re-named just about anything: i and f seem to be commonly used.

  • You can change *.avi to whatever input format you need, such as *.mkv.

do ffmpeg -i "$file" "${file%.avi}".webm;

This will run ffmpeg for each avi. The part in curly braces is using parameter expansion to remove the .avi part of the file name. For example, if an avi file is named dog.avi, then the output will be named dog.webm. If you simply use "$file.webm" for the output, then the output would end up as dog.avi.webm.

What to customize:

  • You can add any ffmpeg options as usual.

  • The %. part should use the same extension as in the for file in section, or you could change %.avi to %.* as a wildcard.

  • .webm can be changed to whatever output container format you need.

If you want the new files to be outputted to a directory named newvids, then make the new directory and change the command to: "newvids/${file%.avi}.webm"

done

This tells the loop that the command has finished, and now $file can refer to the next avi.

Share:
9,425

Related videos on Youtube

SupaKoopaTroopa64
Author by

SupaKoopaTroopa64

Updated on September 18, 2022

Comments

  • SupaKoopaTroopa64
    SupaKoopaTroopa64 almost 2 years

    I am wanting to make a script to transcode all the lossless files from my camera to 3mb/s and change the codec to h.264. I found a command that will export the files as one big file but i want somthing that will keep the name and just put the outputs in another directory. Please put notes that describe what each part of the command does so i can modify it for my needs.

    EDIT

    This works:

    for file in *.avi; do ffmpeg -i "$file" "${file%.avi}".webm; done
    

    i just want to know what each part does so i can modify it for my needs.