ffmpeg your whole library

So I came across a problem recently where I needed to change a large directory structure full of AVI files into webm files with ffmpeg. I wasn't keen on doing this the slow way, so I began to scour the web for a script. I didn't find a complete one, so I had to create my own.

The trick was re-creating the directory structure in "NewFolder" from "Videos" but thankfully, that was easy.

find Videos -type d | sed s/Videos/NewFolder/ | xargs -d'\n' mkdir

The script then uses a fairly simple for loop. It runs the ffmpeg command on every avi file in a folder under Videos in the user home folder using the defaults for webm files.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/bin/bash
#This bash script is used to recursively transcode a
#directory structure of media files.
#It is intended to be modified for your needs!
#DO NOT USE AS IS

# This is a loop that grabs files. Change the path and file
# extension as needed!

for i in /home/user1/Videos/*.avi;
do

# This part is for defining the folder names that are in
# the same directory. In this case, /home/user1/
# Default of the 2 folders is ~/Videos and ~/NewFolder

  o=${i/Videos/NewFolder}

# This part strips the .avi from the end of filename.

  j=${o%.avi}

# This is ffmpeg command. Edit as needed using "$i" as the 
# input file name and "$j" as the output file.
# Leave the quotes intact.
# echo is in place so it shows you what it will do
# This is default so you can be sure it will work.
# Remove echo to actually start ffmpeg

echo  ffmpeg -i "$i" "$j.webm"

done

In my specific case, I actually wanted the output somewhere else, so I simply made "NewFolder" a symlink to the directory I wanted it in. Not the best way I'm sure, but fairly simple. The symlink was placed directly in the home folder.

The script assumes both Videos and NewFolder are in the user's Home directory. Using a symlink (ln -s), you can place the output from ffmepg anywhere you like without having to mess with the script too much. This (hopefully) reduces errors from editing. Enjoy!

Category
Tagcloud
life lessons Open Standards linux bash scripts ffmpeg gaming browser OS ubuntu