Για όσους έχουν 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 &quot;*.avi&quot; | while read I; do ~/bin/convert-video-for-N95.sh &quot;$I&quot;; done</span><br><br>Τι κάνει:<br><br>Διαφίμηση: για όσους αναρωτιούνται τι νόημα έχει να βλέπεις video στην οθονίτσα του κινητού να αναφέρω ότι το N95 έχει audio/video output. Εγώ το χρησειμοποιώ σαν media player (και GPS) για το αυτοκίνητο το οποίο έχει μια οθόνη 7&quot; (ιδανικό στα ταξίδια αν έχεις δυο υιούς 5 και 6 χρονών). Σε 4GB χωράνε περίπου 30 ώρες video σε ικανοποιητικότατη ποιότητα.<br>
<br>ορίστε και το script<br>
<b><span style="font-family: courier new,monospace;">&gt; 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 &quot;USAGE:&quot;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">    echo -n $0 | sed -e &quot;s/^.*\///&quot;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">    echo &quot; input_video [output_filename]&quot;</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=&quot;72k&quot; </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=&quot;0.21&quot;</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=&quot;-threads 0&quot; </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 &quot;Error: $1&quot; &gt;&amp;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 ]] &amp;&amp; usage</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">! [ -s &quot;$1&quot; ] &amp;&amp; error &quot;input file doesn&#39;t exist (or is empty)&quot;</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 &quot;$1&quot; | sed &quot;s/\..\{3\}$/.mp4/&quot;` </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=&quot;$2&quot;</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&#39;t</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">if ! ( echo &quot;$dstfile&quot; | grep &quot;.mp4$&quot; &gt; /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=&quot;$dstfile&quot;&quot;.mp4&quot;</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 &quot;[d] we will write output to $dstfile&quot;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">[ -s &quot;$dstfile&quot; ] &amp;&amp; error \</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">    &quot;Output file $dstfile allready exists - remove it if you want me to proceed&quot;</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 &quot;[d] checking basic video properties of input file $1&quot;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">LINE=`ffmpeg -i &quot;$1&quot; 2&gt;&amp;1 | grep -i &quot;Stream .* Video.*fps&quot;`</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 &quot;[d] ffmpeg -i reports: $LINE&quot;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">! [ `echo &quot;$LINE&quot; | wc -l` == 1 ] &amp;&amp; error &quot;more than one video streams!&quot;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">VFPS=`echo    $LINE | sed -e &quot;s/^.*, *//&quot; -e &quot;s/\..*$//&quot;`</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">SIZE=`echo    $LINE | sed -e &quot;s/\(^.*\),.*$/\1/&quot; -e &quot;s/^.*, *//&quot;` # 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 ]] &amp;&amp; [[ $VWIDTH ]] &amp;&amp; [[ $VHEIGHT ]] ) &amp;&amp; error \</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">    &quot;tcprobe doesn&#39;t report frames or width or height - is this a video?&quot;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">echo &quot;[i] input video frames per sec=$VFPS&quot;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">echo &quot;[i] input video frame size=$VWIDTH x $VHEIGHT&quot;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">OutOfRange $VFPS 5 40 &amp;&amp; error &quot;input frame rate $VFPS is out of sane range&quot;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">OutOfRange $VWIDTH 50 3000 &amp;&amp; error &quot;input width $VWIDTH is out of sane range&quot;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">OutOfRange $VHEIGHT 50 2000 &amp;&amp; error &quot;input height $VHEIGHT is out of sane range&quot;</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 &quot;$VWIDTH*10/$VHEIGHT &quot; | bc`</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">OutOfRange $INPUTASP 10 21 &amp;&amp; error &quot;input aspect ratio is out of sane range&quot;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">InRange $INPUTASP 14 16 &amp;&amp; echo &quot;[i] input aspect ratio is neither close to 16:9 nor to 4:3 - I&#39;ll do my best but check the output ;)&quot;</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 &quot;[i] we are assuming that input video has aspect ratio ~16:9&quot;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">       HEIGHT=&quot;176&quot; </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 &quot;[i] we are assuming that input video has aspect ratio ~4:3&quot;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">       HEIGHT=&quot;240&quot; </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 &quot;scale=4; x=320*$HEIGHT*$VFPS*$BPP;  scale=0 ; x/1000 &quot; | bc`</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">echo &quot;[d] we need $VIDEOBITRATE kpbs for output video&quot;</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=&quot;-s 320x&quot;$HEIGHT&quot; -aspect 4:3 -b $VIDEOBITRATE&quot;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&#39;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 &quot;$1&quot; -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 &#39;blurCplx^(1-qComp)&#39; -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 &quot;$dstfile&quot;</span><br>
<br style="font-family: courier new,monospace;">
<br><br><br>-- <br>&quot;The software is licensed, not sold&quot; -- MICROSOFT LICENSE TERMS <br><br>