After a few months I came back to my any2avi script. The previous version from post #15 wasn’t too good. It produced the files with strong artifacts around the fast moving objects or objects moving in the dark. In some cases after the rescaling a problem concerning non-square pixels appeared. The script didn’t recognize the accepted formats so it performed some unwanted conversions as well.
The new version of the script solves these problems. Thanks to the configuration section you may list all accepted formats in order to avoid unnecessary conversions. You may use the full lists of the video and audio formats or the shortened lists using the regular expressions (see: the script). The parameters and formats which I stored in that section of the script are accepted by my device. You may remove some of them or add the new ones for the best compatibility with your device. If you’d like to produce the most compatible files leave in the configuration section just
mpeg4 video format and
mp3 audio format. The new script uses just ffmpeg program but you should compile it with LAME, X264, FAAC, and XVID enabled.
Have fun!
any2avi
Code:
#!/bin/bash
# any2avi: converts any video file to avi file compatible with most devices
# compile ffmpeg with LAME, X264, FAAC, and XVID enabled:
#
# FAAC=yes XVID=yes ./ffmpeg.SlackBuild
#
# CONFIGURATION SECTION BEGINS HERE
#
accepted_width="720" # highest accepted width
accepted_height="576" # highest accepted height
accepted_video="mpeg4 msmpeg4 mpeg2video mpeg1video" # all accepted video codecs
accepted_audio="mp3 ac3 mp2 wmav2 pcm_s16le" # all accepted audio codecs
#accepted_video="(ms)?mpeg4 mpeg[12]video" # all accepted video codecs
#accepted_audio="mp[23] ac3 wmav2 pcm_s16le" # all accepted audio codecs
#
# CONFIGURATION SECTION ENDS HERE
#
infile="$1"
if [ "$infile" == "" ]
then
cat <<EOF
any2avi: converts any video file to avi file compatible with most devices
any2avi file
EOF
exit
fi
tempfile="/tmp/.ffmpeg.tmp"
accepted_scale=${accepted_width}x${accepted_height}
ffmpeg -i "$infile" 2> $tempfile
if [ ! -e $tempfile ]
then
cat <<EOF
any2avi: cannot create $tempfile file
EOF
exit
fi
video_string=$(grep 'Video: ' $tempfile)
audio_string=$(grep 'Audio: ' $tempfile)
rm $tempfile
orig_vcodec=$(echo $video_string | awk '{print $4}')
orig_vcodec=${orig_vcodec/,/}
orig_acodec=$(echo $audio_string | awk '{print $4}')
orig_acodec=${orig_acodec/,/}
orig_scale=`echo $video_string | sed -E "s/.* ([0-9]+x[0-9]+)[ ,].*/\1/"`
orig_width=$orig_scale
orig_width=${orig_width/x*/}
orig_height=$orig_scale
orig_height=${orig_height/*x/}
orig_aspect=$(echo "scale=2 ; $orig_width/$orig_height" | bc)
if [ $orig_width -le $accepted_width ] && [ $orig_height -le $accepted_height ]
then
video="rejected"
for video_format in $accepted_video
do
if [ "$(echo $orig_vcodec | egrep -w $video_format)" != "" ]
then
video="accepted"
fi
done
audio="rejected"
for audio_format in $accepted_audio
do
if [ "$(echo $orig_acodec | egrep -w $audio_format)" != "" ]
then
audio="accepted"
fi
done
if [ "$video" == "accepted" ] && [ "$audio" == "accepted" ]
then
cat <<EOF
any2avi: no conversion required
# scale = $orig_scale
# aspect = $orig_aspect
# vcodec = $orig_vcodec
# acodec = $orig_acodec
EOF
exit
fi
fi
function count_scale() {
if [ $orig_width -gt $accepted_width ]
then
factor=$(echo "scale=2 ; $orig_width/$accepted_width" | bc)
width=$accepted_width
height=$(echo "$orig_height/$factor" | bc)
height=$(($height/2))
height=$(($height*2))
else
width=$orig_width
height=$orig_height
fi
if [ $height -gt $accepted_height ]
then
factor=$(echo "scale=2 ; $height/$accepted_height" | bc)
height=$accepted_height
width=$(echo "$width/$factor" | bc)
width=$(($width/2))
width=$(($width*2))
fi
scale=${width}x${height}
}
count_scale
aspect=$(echo "scale=2 ; $width/$height" | bc)
bitrate=$(($width*$height*50*25/256/1000))k
function run() {
cat <<EOF
# accepted:
# scale = $accepted_scale
# vcodec = $accepted_video
# acodec = $accepted_audio
# original:
# scale = $orig_scale
# aspect = $orig_aspect
# vcodec = $orig_vcodec
# acodec = $orig_acodec
# used:
# scale = $scale
# aspect = $aspect
# bitrate = $bitrate
# vcodec = mpeg4 (libxvid)
# acodec = mp3 (libmp3lame)
$command
EOF
eval $command
}
command="ffmpeg -i \"$infile\" -codec:v libxvid -b:v $bitrate -aspect $aspect -s $scale -codec:a libmp3lame -b:a 160k -ar 48000 \"$infile.avi\""
run