ipodvidenc + ipodresize

Discussion in 'iPod touch' started by patsissons, Feb 15, 2008.

  1. patsissons

    patsissons New Member

    Joined:
    Jan 30, 2008
    Messages:
    18
    Likes Received:
    0
    Device:
    iPod touch
    In light of the new iPodME tool that was released, I figured I would contribute something of my own to the linux ipod (touch) users.

    I have been slowly tweaking and perfecting this tool for about 2 years. I started out with a simple script and over time continued to add features and tweak settings. Right now these scripts are setup for the ipod touch, but they can easily be switched to the 5G (if that's your thing) or any other device that can play videos.

    The first script is just a utility i use to recalculate optimal size (to preserve the aspect ratio). It's pretty simple, but it works quite well. the script requires that aviresize is installed (avifile-utils on debian/ubuntu)

    ipodresize

    The second script is the encoder script, the idea is that it simplifies the process by making your command as short as possible by using preset values that have aleady been optimized. Note that the preset values are optimized for file size, which means that it will look and sound good on the ipod, but it probably won't look good if it is sized any bigger or used with large speakers. If this is something that is desired routinely, simply update the preset values to that of your liking. This script requires that ffmpeg is installed and properly configured. This can be a little difficult, i have a thread on ubuntu forums that goes through it fairly well but i think it is a little dated, worth a look tho if you are having trouble. Your best bet is to find a binary that is already properly built, im sure there is one somewhere out there.

    ipodvidenc

    Now these scripts are far from idiot proof, i usually trust myself not to intentionally try and break it. That said, you may need to tweak some of the settings to your own liking, but for the most part i would leave them alone. However, if you do think that some value is grossly under or over valued please let me know I am always interested in making my encodes better (or faster).

    NOTE: in version 1.1 of ipodvidenc, you are no longer required to explicity call ipodrename to get size and aspect ratio values, the script will call ipodresize on its own (make sure it is in the PATH).

    Here are two examples on how to use these scripts. The first is a simple one file encode, the second a multi file encode, and the third is a directory encode.

    Code:
    ipodvidenc "Some Awesome Move.avi"
    
    for i in "Movie 1.avi" "Movie 2.avi" "Movie 3.avi"; do ipodvidenc "$i"; done
    
    for i in *.avi; do ipodvidenc "$i"; done
    
    Coming up in version 1.2:
    * better design that will allow >2 pass encodes

    cheers.
  2. patsissons

    patsissons New Member

    Joined:
    Jan 30, 2008
    Messages:
    18
    Likes Received:
    0
    Device:
    iPod touch
    ipodvidenc

    Code:
    #!/bin/bash
    # ipodvidenc
    # Version 1.1
    # Created and maintained by Pat Sissons
    
    # Processing Config
    # THREADS   = number of threads to use (default = 3)
    #             (1 is good for single core, 3 is good for dual core)
    # PASS1     = perform the first pass (default = 1 [= on])
    # PASS2     = perform the second pass (default = 1 [= on])
    THREADS=3
    PASS1=1
    PASS2=1
    
    # general format config
    # PASSLOG   = where the 1st pass data is stored (this gets auto generated or uses the --passlog param)
    # FORMAT    = type of video file, for ipod this must be either mov, mp4, or h264
    PASSLOG=
    FORMAT=h264
    
    # video format config
    # VCODEC    = video codec, generally h264 or mpeg4
    # VBITRATE  = video bit rate (300k looks decent)
    # VSIZE     = video size [WxH]  (320x240 for 5G)
    #                               (640x480 for iphone/touch)
    # ASPECT    = aspect ratio (4:3 is standard)
    # FRAMERATE = fps for the video (ntsc is standard, but 15 cuts the filesize down a lot)
    VCODEC=h264
    VBITRATE=300k
    #VSIZE=640x480
    #ASPECT=4:3
    FRAMERATE=15
    
    # video advanced settings
    # MOTION    = motion estimator (epzs, full, etc...)
    # QPEL      = subpel refinement quality (1 = slow/HQ, 8 = fast/LQ)
    # REFS      = encode quality? (higher values = higher quality but slower)
    # BUFSIZE   = rate control buffer size
    # GOPSIZE   = how many frames between I-Frames
    # BFRAMES   = use 'frames' B-frames (default is 0 [= off])
    # LEVEL     = uses the level spec (this is apparently good for ipod encoding) (default is 13 [= 1.3])
    MOTION=epzs
    QPEL=8
    REFS=1
    BUFSIZE=244
    GOPSIZE=300
    BFRAMES=0
    LEVEL=13
    
    # audio format settings
    # ACODEC    = audio codec (this shouldn't change from aac)
    # AFREQ     = audio frequency (44100 is normal)
    # ABITRATE  = audio bit rate (48 sounds good with standard ipod headphones)
    ACODEC=aac
    AFREQ=44100
    ABITRATE=48
    
    while [ $# -gt 0 ]
    do
        case "$1" in
                --help)
                    echo "Options :"
                    echo -e "\t--help (display this help text)\n"
                    echo -e "\t--disable1 (disable first pass)"
                    echo -e "\t--disable2 (disable second pass)"
                    echo -e "\t--passlog s (default is $PWD/<file_name>.pass)"
                    echo -e "\t--format s (default is h264)"
                    echo -e "\t--vcodec s (default is h264)"
                    echo -e "\t--vbitrate s (default is 300k)"
                    echo -e "\t--vsize s (default is 320x240)"
                    echo -e "\t--aspect s (default is 4:3)"
                    echo -e "\t--framerate s (default is 15)"
                    echo -e "\t--motion s (default is epzs)"
                    echo -e "\t--qpel s (default is 8)"
                    echo -e "\t--refs s (default is 1)"
                    echo -e "\t--bufsize s (default is 244)"
                    echo -e "\t--gopsize s (default is 300)"
                    echo -e "\t--bframes s (default is 0)"
                    echo -e "\t--level s (default is 13)"
                    echo -e "\t--acodec s (default is aac)"
                    echo -e "\t--afreq s (default is 44100)"
                    echo -e "\t--abitrate s (default is 48)"
                    exit
                    ;;
                --disable1)
                    unset PASS1
                    shift 1
                    ;;
                --disable2)
                    unset PASS2
                    shift 1
                    ;;
                --passlog)
                    PASSLOG=$2
                    shift 2
                    ;;
                --format)
                    FORMAT=$2
                    shift 2
                    ;;
                --vcodec)
                    VCODEC=$2
                    shift 2
                    ;;
                --vbitrate)
                    VBITRATE=$2
                    shift 2
                    ;;
                --vsize)
                    VSIZE=$2
                    shift 2
                    ;;
                --aspect)
                    ASPECT=$2
                    shift 2
                    ;;
                --framerate)
                    FRAMERATE=$2
                    shift 2
                    ;;
                --motion)
                    MOTION=$2
                    shift 2
                    ;;
                --qpel)
                    QPEL=$2
                    shift 2
                    ;;
                --refs)
                    REFS=$2
                    shift 2
                    ;;
                --bufsize)
                    BUFSIZE=$2
                    shift 2
                    ;;
                --gopsize)
                    GOPSIZE=$2
                    shift 2
                    ;;
                --bframes)
                    BFRAMES=$2
                    shift 2
                    ;;
                --level)
                    LEVEL=$2
                    shift 2
                    ;;
                --acodec)
                    ACODEC=$2
                    shift 2
                    ;;
                --afreq)
                    AFREQ=$2
                    shift 2
                    ;;
                --abitrate)
                    ABITRATE=$2
                    shift 2
                    ;;
                *)
                    break
                    ;;
        esac
    done
    
    input_file=$1
    if [ -z $VSIZE ]; then
        VSIZE=`ipodresize "${input_file}" c`
    fi
    if [ -z $ASPECT ]; then
        ASPECT=`ipodresize "${input_file}" r`
    fi
    
    output_file=`basename "${input_file}"`
    output_title="`echo "${output_file}" | sed -e 's/\.\w\+$//'`"
    if [ -z $PASSLOG ]; then
        PASSLOG="/tmp/`echo "${output_title}" | sed -e 's/[^A-Za-z0-9]/_/g'`.pass"
    fi
    output_file="${output_title}.mp4"
    
    echo -e "\n${input_file} => ${output_file}\n"
    
    if [ $PASS1 ]; then
        echo -e "ffmpeg -threads ${THREADS} -y -pass 1 -passlogfile \"${PASSLOG}\" -i \"${input_file}\" -vcodec ${VCODEC} -me ${MOTION} -refs ${REFS} -subq ${QPEL} -b ${VBITRATE} -s ${VSIZE} -aspect ${ASPECT} -r ${FRAMERATE} -bf ${BFRAMES} -level ${LEVEL} -g ${GOPSIZE} -f ${FORMAT} /dev/null\n"
    fi
    if [ $PASS2 ]; then
        echo -e "ffmpeg -threads ${THREADS} -y -pass 2 -passlogfile \"${PASSLOG}\" -i \"${input_file}\" -vcodec ${VCODEC} -me ${MOTION} -refs ${REFS} -subq ${QPEL} -b ${VBITRATE} -s ${VSIZE} -aspect ${ASPECT} -r ${FRAMERATE} -bf ${BFRAMES} -level ${LEVEL} -g ${GOPSIZE} -acodec ${ACODEC} -ar ${AFREQ} -ab ${ABITRATE} -title \"${output_title}\" \"${output_file}\"\n"
    fi
    
    if [ $PASS1 ]; then
        ffmpeg -threads ${THREADS} -y -pass 1 -passlogfile "${PASSLOG}" -i "${input_file}" -vcodec ${VCODEC} -me ${MOTION} -refs ${REFS} -subq ${QPEL} -b ${VBITRATE} -s ${VSIZE} -aspect ${ASPECT} -r ${FRAMERATE} -bf ${BFRAMES} -level ${LEVEL} -g ${GOPSIZE} -f ${FORMAT} /dev/null
    fi
    if [ $PASS2 ]; then
        ffmpeg -threads ${THREADS} -y -pass 2 -passlogfile "${PASSLOG}" -i "${input_file}" -vcodec ${VCODEC} -me ${MOTION} -refs ${REFS} -subq ${QPEL} -b ${VBITRATE} -s ${VSIZE} -aspect ${ASPECT} -r ${FRAMERATE} -bf ${BFRAMES} -level ${LEVEL} -g ${GOPSIZE} -acodec ${ACODEC} -ar ${AFREQ} -ab ${ABITRATE} -title "${output_title}" "${output_file}"
    fi
    
  3. patsissons

    patsissons New Member

    Joined:
    Jan 30, 2008
    Messages:
    18
    Likes Received:
    0
    Device:
    iPod touch
    ipodresize

    Code:
    #!/usr/bin/perl
    # aviresize
    # Version 1.0
    # Created and maintained by Pat Sissons
    
    # 5G is 320x240
    # iphone/touch is 640x480
    my $WIDTH = 640;
    my $HEIGHT = 480;
    
    sub print_help
    {
        print <<EOF
    Usage:
        ipodresize <video file> [options]
    
    Options:
        s       = print size
        r       = print ratio
        c       = print cropped size
        h       = print help
        <blank> = print size, ratio, cropped on seperate lines
    EOF
    }
    
    my $output = `avitype "$ARGV[0]" | grep 'WxH='`;
    if ($output =~ m/WxH=(\d+)x(\d+)/)
    {
        my $w = $1;
        my $h = $2;
        my ($size, $ratio, $cropped);
    
        $size = $w . 'x' . $h;
        $ratio = $w/$h;
    
        if ($ratio > $WIDTH/$HEIGHT)
        {
            $r = $WIDTH / $w;
            $h *= $r;
            $w = $WIDTH;
        }
        else
        {
            $r = $HEIGHT / $h;
            $w *= $r;
            $h = $HEIGHT;
        }
    
        $w = int($w);
        $h = int($h);
    
        my $wd = $w % 8;
        my $hd = $h % 8;
        $w -= $wd;
        $h -= $hd;
    
        $cropped = $w . 'x' . $h;
    
        if ($#ARGV == 1)
        {
            if ($ARGV[1] =~ m/h/)
            {
                &print_help;
            }
            else
            {
                print "$size\n" if $ARGV[1] =~ m/s/;
                print "$ratio\n" if $ARGV[1] =~ m/r/;
                print "$cropped\n" if $ARGV[1] =~ m/c/;
            }
        }
        else
        {
            print "$size\n$ratio\n$resize\n";
        }
    }
    else
    {
        &print_help;
    }
    
  4. ligelowbee

    ligelowbee New Member

    Joined:
    Feb 7, 2009
    Messages:
    2
    Likes Received:
    0

Share This Page