HOWTO: μετατροπή video για αναπαραγωγή σε Nokia N95

Nick Demou ndemou at gmail.com
Fri Jun 5 11:40:29 EEST 2009


Για όσους έχουν nokia N95 (και πιθανόν και άλλα παρόμοια) κινητό και θέλουν
να παίζουν videos που έχουν στον ΗΥ τους ορίστε ένα script που έγραψα και
δοκίμασα πετυχημένα σε κάμποσα videos (στην περίπτωση μου ήταν divx σε avi
container είτε με 4:3 είτε με 16:9 aspect ration).

το καλείς έτσι:
   convert-video-for-N95.sh input_video [output_filename]

η αν έχεις πολλά video:
   find -iname "*.avi" | while read I; do ~/bin/convert-video-for-N95.sh
"$I"; done

Τι κάνει:

Διαφίμηση: για όσους αναρωτιούνται τι νόημα έχει να βλέπεις video στην
οθονίτσα του κινητού να αναφέρω ότι το N95 έχει audio/video output. Εγώ το
χρησειμοποιώ σαν media player (και GPS) για το αυτοκίνητο το οποίο έχει μια
οθόνη 7" (ιδανικό στα ταξίδια αν έχεις δυο υιούς 5 και 6 χρονών). Σε 4GB
χωράνε περίπου 30 ώρες video σε ικανοποιητικότατη ποιότητα.

ορίστε και το script
*> cat ~/bin/convert-video-for-N95.sh*
#/bin/sh
# convert a video file to mp4 format suitable for
# playback in nokia n95
#
usage ()
{
    echo "USAGE:"
    echo -n $0 | sed -e "s/^.*\///"
    echo " input_video [output_filename]"
    exit 0
}

############################################
#
#  CONFIGURATION - ALTER ACORDING TO YOUR TASTE
#

# define quality for audio,
# 64k...128k seems reasonable
# 72k is what I use and find OK
AUDIOBITRATE="72k"

# define quality for video,
# 0.15...0.25 seems reasonable
# 0.21 is what I use and find OK
BPP="0.21"

# define extra options for ffmpeg
# I use -threads 0 (fast but PC becomes rather slow)
OPTIONAL="-threads 0"

#
#
############################################

############################################
#
#  FUNCTIONS
#

OutOfRange ()
{
    #requires integer parameters VAL,MIN,MAX
    #returns 0 if VAL is not within range MIN to MAX inclusive
    if [ $1 -lt $2 ] || [ $1 -gt $3 ]  ; then return 0; fi
    return 1
}

InRange ()
{
    #requires integer parameters VAL,MIN,MAX
    #returns 0 if VAL is within range MIN to MAX inclusive
    if [ $1 -lt $2 ] || [ $1 -gt $3 ]  ; then return 1; fi
    return 0
}

error ()
{
    # print message and terminate script
    echo "Error: $1" >&2
    exit 1
}

############################################
#
#  MAIN PROGRAM BEGINS
#

#-------------------------------------------
# sanity check for first parameter (input filename)
#-------------------------------------------
! [[ $1 ]] && usage
! [ -s "$1" ] && error "input file doesn't exist (or is empty)"

#-------------------------------------------
# set the output filename
#-------------------------------------------
if ! [[ $2 ]]
then
    # second parameter is missing so:
    # derive output file name from input file name
    # We replace any existing 3 chr extension with .mp4
    # if no 3chr extension exists code bellow will append it
    dstfile=`echo "$1" | sed "s/\..\{3\}$/.mp4/"`
else
    # second parameter is the output filename
    dstfile="$2"
fi

# output filename MUST end in .mp4
# append it if it doesn't
if ! ( echo "$dstfile" | grep ".mp4$" > /dev/null )
then
    dstfile="$dstfile"".mp4"
fi

echo "[d] we will write output to $dstfile"
[ -s "$dstfile" ] && error \
    "Output file $dstfile allready exists - remove it if you want me to
proceed"

#-------------------------------------------
# read basic video properties (width, height, fps)
#-------------------------------------------
echo "[d] checking basic video properties of input file $1"
LINE=`ffmpeg -i "$1" 2>&1 | grep -i "Stream .* Video.*fps"`
# find Frame Rate, width, height from a line like this
# Stream #0.1: Video: wmv3, yuv420p, 640x480, 25.00 fps(r)
echo "[d] ffmpeg -i reports: $LINE"
! [ `echo "$LINE" | wc -l` == 1 ] && error "more than one video streams!"
VFPS=`echo    $LINE | sed -e "s/^.*, *//" -e "s/\..*$//"`
SIZE=`echo    $LINE | sed -e "s/\(^.*\),.*$/\1/" -e "s/^.*, *//"` # eg
640x480
VWIDTH=`echo  $SIZE | cut -dx -f1`
VHEIGHT=`echo $SIZE | cut -dx -f2`
! ( [[ $VFPS ]] && [[ $VWIDTH ]] && [[ $VHEIGHT ]] ) && error \
    "tcprobe doesn't report frames or width or height - is this a video?"
echo "[i] input video frames per sec=$VFPS"
echo "[i] input video frame size=$VWIDTH x $VHEIGHT"
OutOfRange $VFPS 5 40 && error "input frame rate $VFPS is out of sane range"
OutOfRange $VWIDTH 50 3000 && error "input width $VWIDTH is out of sane
range"
OutOfRange $VHEIGHT 50 2000 && error "input height $VHEIGHT is out of sane
range"


#-------------------------------------------
# based on input video properties derive
# video options for ffmpeg
#-------------------------------------------
INPUTASP=`echo "$VWIDTH*10/$VHEIGHT " | bc`
OutOfRange $INPUTASP 10 21 && error "input aspect ratio is out of sane
range"
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 ;)"
if [ $INPUTASP -gt 14 ]
then
       echo "[i] we are assuming that input video has aspect ratio ~16:9"
       HEIGHT="176"
else
       echo "[i] we are assuming that input video has aspect ratio ~4:3"
       HEIGHT="240"
fi

# we must calculate the video bit rate in kbps to pass it to ffmpeg.
# The formula is:    kbps = W * H * fps * BPP
VIDEOBITRATE=`echo "scale=4; x=320*$HEIGHT*$VFPS*$BPP;  scale=0 ; x/1000 " |
bc`
echo "[d] we need $VIDEOBITRATE kpbs for output video"

# all video options
VIDEOOPTIONS="-s 320x"$HEIGHT" -aspect 4:3 -b $VIDEOBITRATE"k

#-------------------------------------------
# call ffmpeg to do it's job
#-------------------------------------------
echo ffmpeg -y -i "$1" -acodec libfaac -ab $AUDIOBITRATE $VIDEOOPTIONS \
       -vcodec libx264 -qcomp 0.6 -qmin 16 -qmax 51 -qdiff 4 -flags +loop \
       -cmp +chroma -subq 7 -refs 6 -g 250 -keyint_min 25 \
       -rc_eq 'blurCplx^(1-qComp)' -sc_threshold 40 -me_range 12 \
       -i_qfactor 0.71 -directpred 3 $OPTIONAL "$dstfile"




-- 
"The software is licensed, not sold" -- MICROSOFT LICENSE TERMS
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.hellug.gr/pipermail/linux-greek-users/attachments/20090605/94053cae/attachment.htm>


More information about the Linux-greek-users mailing list