[EDIT] Updated with evilsoup's suggestions. I got really awful quality when using h.264 option, went back to the default and got my q number to stay below 5... using '-c:v libx264' yielded q=20 and higher. As stated below you could pass in parameters so that $1 would be input and $2 would be output. For my uses, I will always use the same folders but this is handy to know.
Code:
./convert.sh /dir/input /dir/output
...
in_folder=$(readlink -f $1) #where $1 is /dir/input
out_folder=$(readlink -f $2) #where $2 is /dir/output
evilsoup's changes:
Code:
#!/bin/bash
# convert.sh
# Converts AVI files to FLV files
# If audio bitrate is not detected file is moved to prevent being deleted
# If audio sample rate is 48kHz, sample rate is reduced to 44.1kHz
in_folder=$(readlink -f /home/media/Videos/Original/)
out_folder=$(readlink -f /home/media/Videos/Converted/)
find "$in_folder" -type f -iname "*.avi" -exec readlink -f '{}' \; | while read src
do
# Determine bit rates and audio sample rate
total=$(ffprobe $src 2>&1 | grep "Duration:" | egrep -o '\<[0-9]{3}\>')
audio=$(ffprobe $src 2>&1 | grep "Audio:" | egrep -o '\<[0-9]{3}\>')
hertz=$(ffprobe $src 2>&1 | grep "Audio:" | egrep -o '\<[0-9]{5}\>')
let video="$total"-"$audio"
outfile="$out_folder/$(basename $src .avi)"
if [ "$audio" == "" ]
then
mv "$src" /home/media/Videos/Manual
else
# Conversion
if [ "$hertz" == "48000" ]
then
ffmpeg -i "$src" -b:v "$video"k -c:a libfdk_aac -b:a "$audio"k -ar 44100 -f flv "$outfile".flv -y
else
ffmpeg -i "$src" -b:v "$video"k -c:a libfdk_aac -b:a "$audio"k -ar "$hertz" -f flv "$outfile".flv -y
fi
# Inject cuepoints and remove source file
flvtool2 -U "$outfile".flv
rm "$src"
fi
done
I've been leeching from these forums for years and I feel that I finally have something worthwhile to contribute. I built myself a webserver that is hosting flv videos and I have been using crappy tools for the conversion and most of the time I have to do each one by hand because there is nothing standard with the files I'm using. I finally got fed up with the less than optimal output of these tools and decided to script a way to do the entire conversion with one script and improve overall quality.
Code:
#!/bin/bash
in_folder=/home/media/Videos/Original/
out_folder=/home/media/Videos/Converted/
files=`ls $in_folder`
k=k
flv=.flv
for src in $files
do
total=`ffmpeg -i $in_folder$src 2>&1 | grep "Duration:" | egrep -o '\<[0-9]{3}\>'`
audio=`ffmpeg -i $in_folder$src 2>&1 | grep "Audio:" | egrep -o '\<[0-9]{3}\>'`
hertz=`ffmpeg -i $in_folder$src 2>&1 | grep "Audio:" | egrep -o '\<[0-9]{5}\>'`
let video=$total-$audio
base=${src%%.*}
if [ $hertz == "48000" ]
then
ffmpeg -i $in_folder$src -b:v $video$k -acodec mp3 -b:a $audio$k -ar 44100 -f flv $out_folder$base$flv
else
ffmpeg -i $in_folder$src -b:v $video$k -acodec mp3 -b:a $audio$k -ar $hertz -f flv $out_folder$base$flv
fi
flvtool2 -U $out_folder$base$flv
rm $in_folder$src
done
This was done completely on Ubuntu 12.04 Desktop 32bit.
There are a few assumptions made with this script, namely you are going from avi to flv, I'm pretty sure that mkv or mp4 would likely work I just don't have any on my computer at this time. The next assumption is that ffmpeg is compiled by source. I used this website as a guide, I did not install the optional opus library: http://ffmpeg.org/trac/ffmpeg/wiki/U...mpilationGuide. I also sanitized all my file names prior to the conversion, I'm pretty sure my script will fail as it generates its files array from the output of ls /path/name/ which does not include the escape characters. There is probably an easy way to handle that, I just didn't look into it.
A key thing to take note when you are watching the output of the conversion that the "q=x.x" value should never approach 31, the range is 2 to 31 with 2 being best and 31 being worst. On my VM I gave it 4gb of ram (even though I'm on 32bit OS...) and a single core on my processor and I was able to keep the q value pretty low, somewhere between 2 and 6 with the occasional spike to 16 or so.
The conditional that is questioning the hertz value of the source avi is important because flv files cannot exceed 44100 hertz on their audio track, and from my tests dropping it down from 48k to 44.1k didn't hurt the audio in a noticeable way.
You will also need flvtool2 installed as well, as this will allow you to inject keyframe or cuepoints, not sure which is the correct term, into the flv file to allow you jump to a given point in the stream. This part is extremely CPU intensive and takes along time to complete, open up a new terminal and watch it with top.
You may want to comment out the last line where it is removing the source file from the computer, as you will likely want to have that on hand again if something goes wrong with the script.
Please give me some comments if something could be changed to improve quality or anything like that. I am not a pro at this stuff so I likely won't be able to answer any questions regarding obscure errors but ask away.
Bookmarks