Fuze Video Conversion

I found the following script to convert videos to be compatible with the Sanza Fuze player.  At the center of it is a script posted to the Sanza Forums.  Many thanks to the script author ‘Thomas’.  You can find his helpful posts on his Sanza Forum Profile Page.

Step One:

Ensure mencoder is installed, final processing step does require the use of a windows app using wine, so make sure both are installed.

sudo apt-get install mencoder wine unzip

Step Two:

Grab the freeware AVI-MUX program:  Website Link,  or use the code below.

Extract the zip to your windows ‘program files’ directory as configured in wine.  If you haven’t done any customization it is located at: “~/.wine/drive_c/Program Files”
I downloaded the zip to this location and extracted it at this location.

Accomplish all of this with:

cd ~/.wine/drive_c/Program\ Files

wget http://www.alexander-noe.com/video/amg/AVI-Mux_GUI-1.17.8.zip

unzip AVI-Mux_GUI-1.17.8.zip -d AVI-Mux_GUI-1.17.8

Step Three:

Save the script (At the bottom) into a location that is in your path and set as executable.  I kept the original authors name ‘pvc.sh’

Step Four:

Execute the conversion with:

./pvc.sh input_video.mkv output_name.avi

The Script:

Thomas (the script author) posted his script at the following location for download.

You can download ewelot’s script, and verify file integrity by running

md5sum pvc.sh

which must print the following hash value:

d9b3efdec6a32f1853f59784101d8503  pvc.sh

or copy the script below into a file into your path:

#! /bin/bash

############################################################################
#   pvc.sh [-hvdo] [-x|y] [-p player] [-c codec] [-b vbit] [-a abit]
#          [-k keyint] [-s start] [-l length]
  
#
#   Portable media player video converter for linux.
#
#   The purpose of this script is to convert videos into a format which
#   can be played by portable media players, e.g. the SanDisk Sansa Fuze.
#   It is based on mencoder and AVI-Mux_GUI.exe (under wine). The latter
#   can be found at: http://www.alexander-noe.com/video/amg
#
#   Examples:
#   - convert a video file for the Fuze:
#         pvc.sh anyvideo.mkv file.avi
#   - convert chapters 3 to 5 of title 2 from an anamorpic widescreen DVD:
#         pvc.sh -x dvd://2:3-5 dvdchapter.avi
#
#   Notes:
#   - You can reference selected chapters of DVD titles as
 by using
#     a special syntax of the form 'dvd://title:chapters' e.g. 'dvd://1:4-5'.
#     For a full list of other supported video input sources
#     (like streams e.c.) consult the manpage of mplayer/mencoder
#   - Temporary disk space of about the size of the output file is needed
#     under /tmp.
#   - Seeking ("-s start") is not supported by all
 types.
#   - There are rare cases where avimux-gui cannot handle AVI files
#     created by mencoder.
#
############################################################################
VERSION="1.1"
VINFO="T. Lehmann, Sep. 2009"
COPYRIGHT="(c) Thomas Lehmann, "
LICENSE="GPL v2 or any later version"
PINFO="\
    options:
      h        show this help text
      v        increase verbosity
      d        dry run, display mencoder commands and exit
      o        force to overwrite output file
      p player targeted portable player model (default: Fuze)
                   try '-p list' for a list of supported models
      c codec  video codecs used for encoding (default: xvid)
                   try '-c list' for a list of supported codecs
      b vbit   video bitrate in kbps (default: 600)
      a abit   audio bitrate in kbps (default: 128)
      k keyint video keyframe intervall in frames (default: 15)
      s start  start encoding at given time position (hh:mm:ss)
      l length encode for the given duration (hh:mm:ss)
      x        indicate the source video is 16:9 anamorphic
      y        indicate the source video is  4:3 anamorphic
    parameters:
      input    name of source video to be converted, there is support
               for DVD chapters like 'dvd://1:4-5' which is chapters 4-5
      output   output video file name
"
CHANGELOG="
    1.1  - 02 Sep 2009
        * bugfix: convert WINEPREFIX variable to absolute path
        * bugfix: handle space in path name to the AVI-Mux_GUI.exe
          (thanks to bug report by Jeff Ammons)

    1.0  - 25 Aug 2009
        * added option o to force overwriting output file
        * added support for DVD chapters
        * cleanup and more checks
        * preliminary support for SanDisk Sansa View (untested)

    0.2  - 24 Aug 2009
        * added option d to only display commands
        * removed TOFUZEOPTS in favour of new options x,y,s,l

    0.1  - 23 Aug 2009
        * first test release, based on tofuze() shell function
"

#--------------------
#   user definitions
#--------------------
verbose=0
player=fuze
ac=mp3          # audio codec
srate=44100     # audio sample rate in Hz

#--------------------
#   functions
#--------------------
shorthelp ()
{
  echo "Usage:  $(basename $0)" \
       "[-hvdo] [-x|y] [-p player] [-c codec] [-b vbit] [-a abit]"
  echo "               [-k keyint] [-s start] [-l length]
  "
}

longhelp ()
{
  echo $(basename $0)"   [$VERSION]   $VINFO"
  shorthelp
  printf "$PINFO"
}

error ()
{
  f_str="$1"
  echo "ERROR:  $f_str" >&2
  exit 1
}

#--------------------
#   get options
#--------------------
dryrun="";
player="fuze"   # player model
vc="xvid"       # video codec
vbit=600        # video bitrate in kbps
abit=128        # audio bitrate in kbps
keyint=15       # video keyframe interval
meopts="-ffourcc DX50"
xy=""
overwrite=""
while getopts dp:c:b:a:k:s:l:xyohv c
do
  case $c in
    d)  dryrun=1;;
    o)  overwrite=1;;
    p)  player="$(echo $OPTARG | awk '{print tolower($0)}')";;
    c)  vc="$(echo $OPTARG | awk '{print tolower($0)}')";;
    b)  vbit=$OPTARG;;
    a)  abit=$OPTARG;;
    k)  keyint=$OPTARG;;
    s)  meopts+=" -ss $OPTARG";;
    l)  meopts+=" -endpos $OPTARG";;
    x)  test "$xy" && error "Options x and y are mutually exclusive."
        xy=1
        meopts+=" -aspect 16:9";;
    y)  test "$xy" && error "Options x and y are mutually exclusive."
        xy=1
        meopts+=" -aspect 4:3";;
    h)  longhelp; exit -1;;
    v)  verbose=$(($verbose + 1));;
    \?) shorthelp
        echo "Try '$(basename $0) -h' for more information."
        exit -1;;
  esac
done
shift `expr $OPTIND - 1`

#--------------------
#   get parameter
#--------------------
if [ ! $# = 2 ]
then
    echo "ERROR: Wrong number of parameters."
    shorthelp
    echo "Try '$(basename $0) -h' for more information."
    exit -1
fi
infile="$1"
outfile="$2"

#--------------------
#   checkings
#--------------------
# handling of special source media or streams
if echo "$infile" | grep -q "//"
then
    # some kind of special source media or stream
    # test for special dvd chapter syntax
    if echo "$infile" | grep -q -E "^dvd://[0-9]+:[0-9]+"
    then
        meopts+=" -chapter $(echo "$infile" | cut -d ":" -f3)"
        infile="$(echo "$infile" | cut -d ":" -f1-2)"
    fi
else
    # regular file
    test ! -f "$infile" &&
        error "Input file '$infile' not found."
fi

# output file
test -f "$outfile" && test -z "$overwrite" &&
    error "Output file '$outfile' exists. Use option '-o' to overwrite it."

# programs
for p in mencoder wine winepath
do
    ! type -p $p > /dev/null 2>&1 && error "program $p not in search path"
done

test "$WINEPREFIX" || WINEPREFIX=$HOME/.wine
test ! -d "$WINEPREFIX" &&
  error "Directory WINEPREFIX=\"$WINEPREFIX\" does not exist."
# convert to absolute path
WINEPREFIX="$(cd "$WINEPREFIX" && pwd)"
amgexe="$(find "$WINEPREFIX" -name "AVIMux_GUI.exe" | head -1)"
test -z "$amgexe" &&
  error "Cannot find AVIMux_GUI.exe file.
  You might have to provide the path to the AVIMux_GUI software by
  using the WINEPREFIX environment variable on the command line."

#--------------------
#   start
#--------------------
log=$(mktemp "/tmp/tmp_log_$$.XXXXXX")
amgscript=$(mktemp "/tmp/tmp_amg_$$.XXXXXX")
tmp1=$(mktemp "/tmp/tmp_vid_$$.XXXXXX")

# mencoder message level
meopts+=" -msglevel all=$(expr 3 + $verbose)"

# player specific settings
exit_after_info=""
fps=""
container=""
case $player in
    fuze)   w=224   # width in pixel
            h=176   # height in pixel
            fps=20  # video frames per second
            container="avi"
            ;;
    view)   w=320
            h=240
            fps=30
            container="lavf -lavfopts format=mp4"
            ;;
    test)   w=640
            h=480
            ;;
    list|help)
            echo ""
            echo "  List of supported portable media players:"
            echo ""
            echo "      fuze   - SanDisk Sansa Fuze (default)"
            echo "      view   - SanDisk Sansa View (experimental)"
            echo ""
            exit_after_info=1
            ;;
    \?)     error "The player '$player' is not supported by this program."
            ;;
esac

# combine video options to be passed to mencoder
vopts="-vf pp=li,expand=:::::$w/$h,scale=$w:$h,harddup"
test "$fps" && vopts="-ofps $fps $vopts"
test "$container" && vopts="-of $container $vopts"
case $vc in
    mpeg4)  vcopts="-ovc lavc -lavcopts vcodec=mpeg4:vbitrate=$vbit:vmax_b_frames=0:keyint=$keyint"
            vcopts1="$vcopts:turbo:vpass=1"
            vcopts2="$vcopts:vpass=2";;
    xvid)   vcopts="-ovc xvid -xvidencopts bitrate=$vbit:max_bframes=0:max_key_interval=$keyint"
            vcopts1="$vcopts:turbo:pass=1"
            vcopts2="$vcopts:pass=2";;
    x264)   vcopts="-ovc x264 -x264encopts bitrate=$vbit:bframes=0:keyint=$keyint"
            vcopts1="$vcopts:turbo=1:pass=1"
            vcopts2="$vcopts:pass=2";;
    list|help)
            echo ""
            echo "  List of supported video codecs:"
            echo ""
            echo "      xvid   - Xvid MPEG-4 Part 2 codec (default)"
            echo "      mpeg4  - mpeg4 MPEG-4 Part 2 codec from libavcodec/ffmpeg"
            echo "      x264   - H.264 MPEG-4 Part 10 AVC codec (experimental)"
            echo ""
            exit_after_info=1
            ;;
    \?)     error "The video codec '$vc' is not supported."
            ;;
esac
test "$exit_after_info" && exit 0

# reject invalid combinations of player and codec
test "$player" == "fuze" && test "$vc" == "x264" &&
    error "x264 video codec is not supported by SanDisk Sansa Fuze."

# combine audio options to be passed to mencoder
aopts="-srate $srate -af resample=$srate:0:1,format=s16le"
case $ac in
    mp3)    acopts="-oac mp3lame -lameopts cbr:br=$abit";;
    ac3)    acopts="-oac lavc -lavcopts acodec=ac3:abitrate=$abit";;
    *)      acopts=""; echo "unknown audio codec";;
esac

# mencoder two-pass encoding
msgprefix="
# $(basename $0) is executing:
"
test "$dryrun" && msgprefix=""
rm -f divx2pass.log
echo "$msgprefix\
    mencoder $meopts $vopts $vcopts1 $aopts $acopts \"$infile\" -o \"$tmp1\" 2>$log
"
if [ ! "$dryrun" ]
then
    mencoder $meopts $vopts $vcopts1 $aopts $acopts "$infile" -o "$tmp1" 2>$log
fi
err=$?
test ! $err -eq 0 &&
  cat $log &&
  error "mencoder failed at first pass with error $err."

echo "$msgprefix\
    mencoder $meopts $vopts $vcopts2 $aopts $acopts \"$infile\" -o \"$tmp1\" 2>>$log
"
if [ ! "$dryrun" ]
then
    mencoder $meopts $vopts $vcopts2 $aopts $acopts "$infile" -o "$tmp1" 2>>$log
fi
err=$?
test ! $err -eq 0 &&
  cat $log &&
  error "mencoder failed at second pass with error $err."

if [ "$player" == "fuze" ]
then
# create AVIMux_GUI script file
cat < $amgscript
CLEAR
LANGUAGE English
LOAD $(winepath -w "$tmp1")
SELECT FILE 1
ADD VIDEOSOURCE
SET OPTION CLOSEAPP 1
SET OPTION DONEDLG 0
SET OPTION OVERWRITEDLG 0
SET INPUT OPTIONS
WITH SET OPTION
UNBUFFERED 1
OVERLAPPED 1
ADD_IMMED 0
ADD_IMMED 1
USE CACHE 1
AVI FORCE MP3VBR 0
MP3 VERIFY CBR ASK
MP3 VERIFY RESDLG 0
M2F2 CRC 1
AVI FIXDX50 1
AVI IGNLCHUNKS OFF
AVI TRY2FIXLCHUNKS 0
WITH CHAPTERS
IMPORT 1
FROMFILENAMES 0
END WITH
END WITH
SET OUTPUT OPTIONS
WITH SET OPTION
UNBUFFERED 1
THREAD 1
LOGFILE 0
STDOUTPUTFMT AVI
END WITH
DESELECT AUDIO 0
WITH SET OPTION
END WITH
DESELECT SUBTITLE 0
WITH SET OPTION
NO AUDIO 0
ALL AUDIO 1
NO SUBTITLES 0
ALL SUBTITLES 1
OPENDML 1
LEGACY 1
RECLISTS 0
FRAMES 0
PRELOAD 0
MP3 CBR FRAMEMODE 0
MAXFILESIZE OFF
AVI AC3FPC 2
AVI MP3FPC 1
AVI DTSFPC 2
AVI ADDJUNKBEFOREHEADERS 0
OGG PAGESIZE 65025
AVI RIFFAVISIZE 999
AVI HAALIMODE 0
OVERLAPPED 0
MAXFILESIZE 2030
MAXFILES OFF
NUMBERING OFF
NUMBERING $Name ($Nbr)
AUDIO INTERLEAVE 2 FR
SPLIT POINTS OFF
STDIDX 4000 FRAMES
PRLAC BYTE
END WITH
WITH SET OPTION MKV
LACE 3
LACESIZE general 1 500
LACESIZE mp3 1 500
LACESIZE ac3 1 200
LACESIZE dts 1 100
LACESIZE aac 1 200
LACESIZE vorbis 1 50
CLUSTERSIZE 512
CLUSTERTIME 30000
CLUSTERINDEX 0
HARDLINKING 0
NONCLUSTERINDEXMODE 1
HEADERSIZE 0
HEADERSTRIPPING 0
USE_A_AAC 0
PREVCLUSTERSIZE 1
CLUSTERPOSITION 1
LIMIT1STCLUSTER 1
DISPWH 1
CUES 1
CUES VIDEO 1
CUES AUDIO 1
CUES SUBS 1
CUES AUTOSIZE 1
CUES MINIMUMINTERVAL 2000
CUES SIZERATIO 20
CUES TARGETSIZERATIO 980
CUES WRITEBLOCKNUMBER 1
CUES AUDIO ONLY_AUDIO_ONLY 0
TIMECODESCALE MKA 10000
TIMECODESCALE MKV 500000
FORCE_V1 1
FORCE_V2 0
FLOAT_WIDTH 32
RANDOMIZE_ORDER 1
2ND_COPY_OF_TRACKS 1
END WITH
START $(winepath -w "$outfile")
EOScript

# run AVIMux_GUI via wine
echo "$msgprefix\
    wine \"$amgexe\" \"$(winepath -w "$amgscript")\"
"
if [ ! "$dryrun" ]
then
    wine "$amgexe" "$(winepath -w "$amgscript")"
fi

else
   mv "$tmp1" "$outfile"
fi

#--------------------
#   clean-up
#--------------------
for f in $log $amgscript $tmp1
do
  test -f $f && rm $f
done
exit 0

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>