#! /bin/bash

# Copyright 2004, 2005, 2006, 2007 Alexandre Oliva <oliva@lsd.ic.unicamp.br>

# Convert each input media file into a separate Ogg Media file.  Uses
# mplayer to extract the video and audio streams, and theora_encode to
# create the Ogg Media file.

# Some useful flags can be passed to mplayer decoders by setting
# MPLAYER_VIDEO_FLAGS and MPLAYER_AUDIO_FLAGS.  Useful video filters
# are -vf crop=320:240:0:0 -vf expand=-1:-2:0:0


# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, a copy can be downloaded from
# http://www.gnu.org/copyleft/gpl.html, or by writing to the Free
# Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
# MA 02111-1307, USA.

: ${MPLAYER=mplayer -quiet}
: ${touch=:} # touch

status=0

for f
do
  basename=`echo "$f" | sed 's,\.[^.]*$,,'`
  test ! -f "$basename".ogm ||
    { echo "$basename".ogm exists, skipping "$f"; continue; }
  test ! -f "$basename".wav || { echo "$basename".wav exists; exit 1; }
  test ! -f "$basename".Twav || { echo "$basename".Twav exists; exit 1; }
  test ! -f "$basename".yuv || { echo "$basename".yuv exists; exit 1; }
  test ! -f "$basename".Togm || { echo "$basename".Togm exists; exit 1; }
  audio= video= encode=
  trap "rm -f "$basename".wav "$basename".yuv "$basename".Togm "$basename".Twav; kill $audio $video $encode" 0 1 2 15
  mkfifo -m 600 "$basename".wav
  mkfifo -m 600 "$basename".yuv
  #$MPLAYER -ao pcm:file="$basename".wav -vo null "$f" &
  $MPLAYER $MPLAYER_AUDIO_FLAGS -ao pcm:file="$basename".wav,fast -vo null -vc null "$f" &
  audio=$!
  $MPLAYER $MPLAYER_VIDEO_FLAGS -ao null -nosound -vo yuv4mpeg:file="$basename".yuv "$f" &
  video=$!
  theora_encode "$basename".wav "$basename".yuv > "$basename".Togm &
  encode=$!
  while sleep 5; test ! -s "$basename".Togm; do
    kill -0 $audio || {
      wait $audio
      audio=
      break
    }
    kill -0 $video || {
      wait $video
      video=
      break
    }
    kill -0 $encode || {
      wait $encode
      encode=
      break
    }
  done
  if test ! -s "$basename".Togm; then
    if test -z "$audio" && test -n "$video"; then
      rm -f "$basename".Togm
      $MPLAYER $MPLAYER_AUDIO_FLAGS -ao pcm:file="$basename".Twav -vo null "$f"
      kill $video $encode
      video= encode=
      wait
      if test ! -s "$basename".Twav; then
        echo Audio decoder died, trying video only
        rm -f "$basename".wav "$basename".yuv "$basename".Togm
        mkfifo -m 600 "$basename".yuv
        $MPLAYER $MPLAYER_VIDEO_FLAGS -ao null -nosound -vo yuv4mpeg:file="$basename".yuv "$f" &
        video=$!
        theora_encode "$basename".yuv > "$basename".Togm &
        encode=$!
      else
        echo Audio decoder died
        rm -f "$basename".Togm
      fi
    elif test -z "$video"; then
      rm -f "$basename".Togm
      echo Video decoder died
      kill $audio $encode
      audio= encode=
      wait
    elif test -z "$encode"; then
      rm -f "$basename".Togm
      echo Video encoder died
      kill $audio $video
      audio= video=
      wait
    fi
    rm -f "$basename".Twav
  fi
  wait $audio $video $encode
  if test -s "$basename".Togm; then
    $touch -r "$f" "$basename".Togm &&
    mv "$basename".Togm "$basename".ogm &&
    $touch -r "$f" "$basename".ogm
  else
    rm -f "$basename".Togm
    status=1
  fi
  rm -f "$basename".wav "$basename".yuv
  trap "" 0 1 2 15
done

exit $status
