Για όσους έχουν nokia N95 (και πιθανόν και άλλα παρόμοια) κινητό και θέλουν να παίζουν videos που έχουν στον ΗΥ τους ορίστε ένα script που έγραψα και δοκίμασα πετυχημένα σε κάμποσα videos (στην περίπτωση μου ήταν divx σε avi container είτε με 4:3 είτε με 16:9 aspect ration). <br>
<br>το καλείς έτσι:<br><span style="font-family: courier new,monospace;"> convert-video-for-N95.sh </span><span style="font-family: courier new,monospace;">input_video [output_filename]</span><br><br>η αν έχεις πολλά video:<br>
<span style="font-family: courier new,monospace;"> find -iname "*.avi" | while read I; do ~/bin/convert-video-for-N95.sh "$I"; done</span><br><br>Τι κάνει:<br><br>Διαφίμηση: για όσους αναρωτιούνται τι νόημα έχει να βλέπεις video στην οθονίτσα του κινητού να αναφέρω ότι το N95 έχει audio/video output. Εγώ το χρησειμοποιώ σαν media player (και GPS) για το αυτοκίνητο το οποίο έχει μια οθόνη 7" (ιδανικό στα ταξίδια αν έχεις δυο υιούς 5 και 6 χρονών). Σε 4GB χωράνε περίπου 30 ώρες video σε ικανοποιητικότατη ποιότητα.<br>
<br>ορίστε και το script<br>
<b><span style="font-family: courier new,monospace;">> cat ~/bin/convert-video-for-N95.sh</span></b><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">#/bin/sh</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"># convert a video file to mp4 format suitable for </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"># playback in nokia n95</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">#</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">usage ()</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">{</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> echo "USAGE:"</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> echo -n $0 | sed -e "s/^.*\///"</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> echo " input_video [output_filename]"</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> exit 0</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">}</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">############################################</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">#</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"># CONFIGURATION - ALTER ACORDING TO YOUR TASTE</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">#</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"># define quality for audio, </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"># 64k...128k seems reasonable</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"># 72k is what I use and find OK</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">AUDIOBITRATE="72k" </span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"># define quality for video, </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"># 0.15...0.25 seems reasonable</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"># 0.21 is what I use and find OK</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">BPP="0.21"</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"># define extra options for ffmpeg</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"># I use -threads 0 (fast but PC becomes rather slow)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">OPTIONAL="-threads 0" </span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">#</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">#</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">############################################</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">############################################</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">#</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"># FUNCTIONS</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">#</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">OutOfRange ()</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">{</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> #requires integer parameters VAL,MIN,MAX</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> #returns 0 if VAL is not within range MIN to MAX inclusive</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> if [ $1 -lt $2 ] || [ $1 -gt $3 ] ; then return 0; fi</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> return 1</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">}</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">InRange ()</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">{</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> #requires integer parameters VAL,MIN,MAX</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> #returns 0 if VAL is within range MIN to MAX inclusive</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> if [ $1 -lt $2 ] || [ $1 -gt $3 ] ; then return 1; fi</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> return 0</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">}</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">error ()</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">{</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> # print message and terminate script</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> echo "Error: $1" >&2 </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> exit 1</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">}</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">############################################</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">#</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"># MAIN PROGRAM BEGINS</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">#</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">#-------------------------------------------</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"># sanity check for first parameter (input filename)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">#-------------------------------------------</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">! [[ $1 ]] && usage</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">! [ -s "$1" ] && error "input file doesn't exist (or is empty)"</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">#-------------------------------------------</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"># set the output filename</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">#-------------------------------------------</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">if ! [[ $2 ]] </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">then</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> # second parameter is missing so:</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> # derive output file name from input file name</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> # We replace any existing 3 chr extension with .mp4</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> # if no 3chr extension exists code bellow will append it</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> dstfile=`echo "$1" | sed "s/\..\{3\}$/.mp4/"` </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">else</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> # second parameter is the output filename</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> dstfile="$2"</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">fi</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"># output filename MUST end in .mp4 </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"># append it if it doesn't</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">if ! ( echo "$dstfile" | grep ".mp4$" > /dev/null ) </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">then </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> dstfile="$dstfile"".mp4"</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">fi</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">echo "[d] we will write output to $dstfile"</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">[ -s "$dstfile" ] && error \</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> "Output file $dstfile allready exists - remove it if you want me to proceed"</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">#-------------------------------------------</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"># read basic video properties (width, height, fps)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">#-------------------------------------------</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">echo "[d] checking basic video properties of input file $1"</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">LINE=`ffmpeg -i "$1" 2>&1 | grep -i "Stream .* Video.*fps"`</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"># find Frame Rate, width, height from a line like this</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"># Stream #0.1: Video: wmv3, yuv420p, 640x480, 25.00 fps(r)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">echo "[d] ffmpeg -i reports: $LINE"</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">! [ `echo "$LINE" | wc -l` == 1 ] && error "more than one video streams!"</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">VFPS=`echo $LINE | sed -e "s/^.*, *//" -e "s/\..*$//"`</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">SIZE=`echo $LINE | sed -e "s/\(^.*\),.*$/\1/" -e "s/^.*, *//"` # eg 640x480</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">VWIDTH=`echo $SIZE | cut -dx -f1`</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">VHEIGHT=`echo $SIZE | cut -dx -f2`</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">! ( [[ $VFPS ]] && [[ $VWIDTH ]] && [[ $VHEIGHT ]] ) && error \</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> "tcprobe doesn't report frames or width or height - is this a video?"</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">echo "[i] input video frames per sec=$VFPS"</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">echo "[i] input video frame size=$VWIDTH x $VHEIGHT"</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">OutOfRange $VFPS 5 40 && error "input frame rate $VFPS is out of sane range"</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">OutOfRange $VWIDTH 50 3000 && error "input width $VWIDTH is out of sane range"</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">OutOfRange $VHEIGHT 50 2000 && error "input height $VHEIGHT is out of sane range"</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">#-------------------------------------------</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"># based on input video properties derive</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"># video options for ffmpeg</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">#-------------------------------------------</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">INPUTASP=`echo "$VWIDTH*10/$VHEIGHT " | bc`</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">OutOfRange $INPUTASP 10 21 && error "input aspect ratio is out of sane range"</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">InRange $INPUTASP 14 16 && echo "[i] input aspect ratio is neither close to 16:9 nor to 4:3 - I'll do my best but check the output ;)"</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">if [ $INPUTASP -gt 14 ]</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">then </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> echo "[i] we are assuming that input video has aspect ratio ~16:9"</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> HEIGHT="176" </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">else</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> echo "[i] we are assuming that input video has aspect ratio ~4:3"</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> HEIGHT="240" </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">fi</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"># we must calculate the video bit rate in kbps to pass it to ffmpeg. </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"># The formula is: kbps = W * H * fps * BPP</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">VIDEOBITRATE=`echo "scale=4; x=320*$HEIGHT*$VFPS*$BPP; scale=0 ; x/1000 " | bc`</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">echo "[d] we need $VIDEOBITRATE kpbs for output video"</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"># all video options</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">VIDEOOPTIONS="-s 320x"$HEIGHT" -aspect 4:3 -b $VIDEOBITRATE"k</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">#-------------------------------------------</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"># call ffmpeg to do it's job</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">#-------------------------------------------</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">echo ffmpeg -y -i "$1" -acodec libfaac -ab $AUDIOBITRATE $VIDEOOPTIONS \</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> -vcodec libx264 -qcomp 0.6 -qmin 16 -qmax 51 -qdiff 4 -flags +loop \</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> -cmp +chroma -subq 7 -refs 6 -g 250 -keyint_min 25 \</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> -rc_eq 'blurCplx^(1-qComp)' -sc_threshold 40 -me_range 12 \</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> -i_qfactor 0.71 -directpred 3 $OPTIONAL "$dstfile"</span><br>
<br style="font-family: courier new,monospace;">
<br><br><br>-- <br>"The software is licensed, not sold" -- MICROSOFT LICENSE TERMS <br><br>