Ubuntu: How do i transcode all files in a directory with ffmpeg


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.


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.


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"


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

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »