Shell Scripts

I’ll be adding more to this page down the road, but here’s one that I find useful.

makeiso.sh


Here’s a script I created to change my dvd rips into single iso files. I have it run every half hour to check for new files. It works pretty well at this point, but some tweaking will most likely occur.
Note: /mnt/raid/dump is the folder that stores my movies in folder format (VIDEO_TS/*.vob, etc), while /mnt/raid/media/movies is the output folder that stores the iso’s
———————————————————————————-

for directory in /mnt/raid/dump/*
do
if [ -d "$directory" ] ; then
echo "Taking a look at" ${directory#"/mnt/raid/dump/"}
newest=$(ls -t "$directory" | head -1)
age=$(expr $(date +%s) - $(date -r "$directory/$newest" +%s))
echo $newest "is the most recent file at" $age "seconds old"
if [ $age -gt "1000" ] ; then
echo "it's long enough"
if ! test -d "$directory/VIDEO_TS" ; then
mv "$directory" /mnt/raid/dump/VIDEO_TS && mkdir "$directory"
mv /mnt/raid/dump/VIDEO_TS "$directory/VIDEO_TS"
echo "Created VIDEO_TS..."
fi
mkisofs -dvd-video -udf -o "/mnt/raid/media/movies/${directory#"/mnt/raid/dump/"}.iso" "$dire ctory" 2>&1 | grep -v estimate >> /var/log/makeiso.log
if [ -s "/mnt/raid/media/movies/${directory#"/mnt/raid/dump/"}.iso" ] ; then
echo "Deleting ${directory#"/mnt/raid/dump/"}..."
sudo rm -r "$directory" "/mnt/raid/finished_movies/"
echo "Completed"
echo "Finished:" $(date)
echo "---------------------------------------------------"
fi
else
echo "Too soon to make" ${directory#"/mnt/raid/dump/"}
echo "Finished:" $(date)
echo "---------------------------------------------------"
fi
fi
done

ipod_vid.sh


This script converts videos to the ipod format via ffmpeg. It isn’t perfect, but it works well enough, and I was happy with the aspect ratio work on my part.
——————————–


#!/bin/bash

#video codec (or xvid)
VC=”h264″
#video bitrate (1500 max)
VB=”1000k”
#audio codec (or mp3)
AC=”aac”
#audio bitrate (160 max)
AB=”128k”
#ipod presets (YOU SHOULDN’T HAVE TO CHANGE THESE)
DEF=”-threads 3 -f mp4 -level 30 -r 24000/1001 -bufsize 10000k -coder 0 \
-g 300 -bf 0 -coder 0 -maxrate 1400k -ac 2 -passlogfile logfile ”
QUAL=” -deblockalpha 0 -deblockbeta 0 -parti4x4 1 -partp8x8 1 -partb8x8 1″
#output directory

OUT=”/mnt/raid/media/ipod_videos/”

for file in “$1″
do
echo $file

#—————————————————-
#gets file name minus location and type
#—————————————————-
VIDFILE=”$(echo $file | awk -F / ‘{print $NF}’)”
VIDTYPE=”$(echo $VIDFILE | awk -F ‘.’ ‘{print $NF}’)”
VIDNAME=”${VIDFILE%”.$VIDTYPE”}”
settings=”$DEF -vcodec $VC -b $VB -acodec $AC -ab $AB”

#generates ipod friendly res with same aspect ratio
#—————————————————-
orig_res=$(ffmpeg -i “$file” 2>&1 | grep “Video: ” | awk -F ‘ ‘ ‘{print $6}’ | awk -F , ‘{print $1}’)
echo “originalres $orig_res”
width=$(echo $orig_res | awk -F ‘x’ ‘{print $1}’)
height=$(echo $orig_res | awk -F ‘x’ ‘{print $2}’)
echo “beforeratio $width x $height”
ratio=$(echo “scale=5; $width/$height” | bc)
echo “afterratio” $ratio

if [ $(echo “$ratio > 1.33333″ | bc ) = 1 ] ; then
width_final=”640”
height_mod=$(echo “scale=0; $width_final/$ratio” | bc)
if [ $(($height_mod%16)) == “0” ] ; then
height_final=$height_mod
elif [ $(($height_mod%16)) -gt 8 ] ; then
height_final=$(($height_mod+(16-($height_mod%16))))
else
height_final=$(($height_mod-($height_mod%16)))
fi
echo “video too wide”
elif [ $(echo “$ratio < 1.33333″ | bc ) = 1 ] ; then
height_final=”480”
width_mod=$(echo “scale=0; $height_final/$ratio” | bc)
if [ $(($width_mod%16)) == 0 ] ; then
width_final=$width_mod
elif [ $(($width_mod%16)) -gt 8 ] ; then
width_final=$(($width_mod+(16-($width_mod%16))))
else
width_final=$(($width_mod-($width_mod%16)))
fi
echo “video too narrow”
else
width_final=”640″
height_final=”480″
fi
echo $width_final $height_final

#final settings build
#—————————————————-
settings=”$DEF $QUAL -vcodec $VC -b $VB -acodec $AC -ab $AB -s $width_final”x”$height_final”
echo $width_final”x”$height_final

#begins encode
#—————————————————-
echo “——————–”
touch /tmp/ffmpeg.log
nice -n 8 ffmpeg -y -i “$file” -an $settings -pass 1 “$OUT$VIDNAME.mp4” #> /tmp/ffmpeg.log 2>&1 &

rm “$OUT$VIDNAME.mp4”
nice -n 8 ffmpeg -y -i “$file” $settings -pass 2 “$OUT$VIDNAME.mp4” #> /tmp/ffmpeg.log 2>&1 &

echo finishing…
rm -f ffmpeg2pass-0.log x264_2pass.log logfile-0.log /tmp/ffmpeg.log
done

Powered by WordPress with GimpStyle Theme design by Horacio Bella.
Entries and comments feeds. Valid XHTML and CSS.