LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-16-2009, 12:40 PM   #1
True`Colors
LQ Newbie
 
Registered: Mar 2007
Location: Urziceni - Romania
Distribution: Fedora 16
Posts: 12

Rep: Reputation: 0
Bash script for transcoding from several video formats into Theora ogv


Stolen code from another script and modified:

Code:
#!/bin/bash

########################################################################
# User-Defined Variables:

# Where do you want your transcoded files to go?

TRANSCODED_DIR=${HOME}/ogv

########################################################################
# Non-User-Defined Variables:

# This part sorts out the copying of subdirectories in the tree
TOP_WORKING_DIR=`pwd`
TOP_WORKING_DIR_LENGTH=`expr length "$TOP_WORKING_DIR"`
let "PATH_CUT_LENGTH=$TOP_WORKING_DIR_LENGTH+2"

# This variable is set at the command line - avi2ogv, wmv2ogv, mp42ogv or flv2ogv
OUTPUT_TYPE=$1

########################################################################
# The transcoding function for transcoding AVI to OGV using the ffmpeg2theora encoder

avi2ogv ()
{
 EAC_LOG=`basename "$1" .log`
 if [ "$1" == "$EAC_LOG" ]
 then
  echo "Transcoding "$1" to $OUTPUT_TYPE"
  CURRENT_WORKING_DIR=`pwd`
  MAGIC_PATH=`echo "$CURRENT_WORKING_DIR" | cut -b "$PATH_CUT_LENGTH"-`
  TOTAL_PATH="$TRANSCODED_DIR/$MAGIC_PATH"
  mkdir -p "$TOTAL_PATH"
  newname="`basename "$1" .avi`.ogv"

ffmpeg -i "$1" -an -vcodec rawvideo -pix_fmt yuv420p -f yuv4mpegpipe - | ffmpeg2theora --optimize -x 384 -y 288 -F 25 -f yuv4mpegpipe - -o /tmp/tmp.ogv

ffmpeg -i "$1" -vn -acodec pcm_s16le -f wav - | oggenc --resample 22050 -o /tmp/tmp.ogg -

oggzmerge /tmp/tmp.ogv /tmp/tmp.ogg -o "$TOTAL_PATH/$newname"

 else
  cp "$1" "$TOTAL_PATH/.."
  echo "" >> "$TOTAL_PATH/../$1"
  echo "**********" >> "$TOTAL_PATH/../$1"
 fi
 return 0
}

# The transcoding function for transcoding WMV to OGV using the ffmpeg2theora encoder

wmv2ogv ()
{
 EAC_LOG=`basename "$1" .log`
 if [ "$1" == "$EAC_LOG" ]
 then
  echo "Transcoding "$1" to $OUTPUT_TYPE"
  CURRENT_WORKING_DIR=`pwd`
  MAGIC_PATH=`echo "$CURRENT_WORKING_DIR" | cut -b "$PATH_CUT_LENGTH"-`
  TOTAL_PATH="$TRANSCODED_DIR/$MAGIC_PATH"
  mkdir -p "$TOTAL_PATH"
  newname="`basename "$1" .wmv`.ogv"

ffmpeg -i "$1" -an -vcodec rawvideo -pix_fmt yuv420p -f yuv4mpegpipe - | ffmpeg2theora --optimize -x 384 -y 288 -F 25 -f yuv4mpegpipe - -o /tmp/tmp.ogv

ffmpeg -i "$1" -vn -acodec pcm_s16le -f wav - | oggenc --resample 22050 -o /tmp/tmp.ogg -

oggzmerge /tmp/tmp.ogv /tmp/tmp.ogg -o "$TOTAL_PATH/$newname"

 else
  cp "$1" "$TOTAL_PATH/.."
  echo "" >> "$TOTAL_PATH/../$1"
  echo "**********" >> "$TOTAL_PATH/../$1"
 fi
 return 0
}

# The transcoding function for transcoding MP4 to OGV using the ffmpeg2theora encoder

mp42ogv ()
{
 EAC_LOG=`basename "$1" .log`
 if [ "$1" == "$EAC_LOG" ]
 then
  echo "Transcoding "$1" to $OUTPUT_TYPE"
  CURRENT_WORKING_DIR=`pwd`
  MAGIC_PATH=`echo "$CURRENT_WORKING_DIR" | cut -b "$PATH_CUT_LENGTH"-`
  TOTAL_PATH="$TRANSCODED_DIR/$MAGIC_PATH"
  mkdir -p "$TOTAL_PATH"
  newname="`basename "$1" .mp4`.ogv"

ffmpeg -i "$1" -an -vcodec rawvideo -pix_fmt yuv420p -f yuv4mpegpipe - | ffmpeg2theora --optimize -x 384 -y 288 -F 25 -f yuv4mpegpipe - -o /tmp/tmp.ogv

ffmpeg -i "$1" -vn -acodec pcm_s16le -f wav - | oggenc --resample 22050 -o /tmp/tmp.ogg -

oggzmerge /tmp/tmp.ogv /tmp/tmp.ogg -o "$TOTAL_PATH/$newname"

 else
  cp "$1" "$TOTAL_PATH/.."
  echo "" >> "$TOTAL_PATH/../$1"
  echo "**********" >> "$TOTAL_PATH/../$1"
 fi
 return 0
}

# The transcoding function for transcoding FLV to OGV using the ffmpeg2theora encoder

flv2ogv ()
{
 EAC_LOG=`basename "$1" .log`
 if [ "$1" == "$EAC_LOG" ]
 then
  echo "Transcoding "$1" to $OUTPUT_TYPE"
  CURRENT_WORKING_DIR=`pwd`
  MAGIC_PATH=`echo "$CURRENT_WORKING_DIR" | cut -b "$PATH_CUT_LENGTH"-`
  TOTAL_PATH="$TRANSCODED_DIR/$MAGIC_PATH"
  mkdir -p "$TOTAL_PATH"
  newname="`basename "$1" .flv`.ogv"

ffmpeg -i "$1" -an -vcodec rawvideo -pix_fmt yuv420p -f yuv4mpegpipe - | ffmpeg2theora --optimize -x 384 -y 288 -F 25 -f yuv4mpegpipe - -o /tmp/tmp.ogv

ffmpeg -i "$1" -vn -acodec pcm_s16le -f wav - | oggenc --resample 22050 -o /tmp/tmp.ogg -

oggzmerge /tmp/tmp.ogv /tmp/tmp.ogg -o "$TOTAL_PATH/$newname"

 else
  cp "$1" "$TOTAL_PATH/.."
  echo "" >> "$TOTAL_PATH/../$1"
  echo "**********" >> "$TOTAL_PATH/../$1"
 fi
 return 0
}

# Function for finding files recursively

recursive ()
{
 for i in *.avi
 do
  if [ -d "$i" ]
  then
   cd "$i"
   recursive
   cd ..
  else

   if [ "avi2ogv" == "$OUTPUT_TYPE" ]
   then
    avi2ogv "$i"
   elif [ "wmv2ogv" == "$OUTPUT_TYPE" ]
   then
    wmv2ogv "$i"
   elif [ "mp42ogv" == "$OUTPUT_TYPE" ]
   then
    mp42ogg "$i"
   elif [ "flv2ogv" == "$OUTPUT_TYPE" ]
   then
    flv2ogv "$i"
   else
    echo "Output type not recognized - please specify \"avi2ogv\", \"wmv2ogv\", \"mp42ogv\", or \"flv2ogv\""
   fi

  fi
 done
}

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

# The main body of the script...all it does is call the recursive function
recursive

########################################################################
# End of script
########################################################################
 
Old 01-17-2009, 07:21 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Looks like a script that could be useful to some. I have some suggestions for you for cleaning it up a bit.


First of all, the use of `backquotes` is discouraged these days. You should enclose your commands in $() instead:

Code:
TOP_WORKING_DIR=$(pwd)
(BTW, the pwd is also stored in its own shell variable, so you can get the same output by simply using "$PWD").

You can get the length of a variable string with ${#VAR}:

Code:
TOP_WORKING_DIR_LENGTH=${#TOP_WORKING_DIR}
You can do arithmetic inside of $(()) instead of using let:

Code:
PATH_CUT_LENGTH=$((TOP_WORKING_DIR_LENGTH+2))
You strip off everything in a string up to and including the final slash with ${VAR##*/} (this will let you avoid the above arithmetic entirely):

Code:
MAGIC_PATH=${CURRENT_WORKING_DIR##*/}
"$(basename $PWD)" will also work.

The preceding ${VAR} patterns are all examples of parameter substitution.

Finally, in your final function, I think I'd use a case statement instead of if. It's cleaner and you only have to call the variable match once:

Code:
case "$OUTPUT_TYPE" in

avi2ogv ) avi2ogv $i  ;;

wmv2ogv ) wmv2ogv $i  ;;

mp42ogv ) mp42ogv $i  ;;

flv2ogv ) flv2ogv $i  ;;

* )  echo "Output type not recognized - please specify \"avi2ogv\", \"wmv2ogv\", \"mp42ogv\", or \"flv2ogv\""  ;;

esac

Hope these help!


Edit: I just realized we could make the case statement even shorter:
Code:
case "$OUTPUT_TYPE" in

avi2ogv | wmv2ogv | mp42ogv | flv2ogv ) $OUTPUT_TYPE $i  ;;

* )  echo "Output type not recognized - please specify \"avi2ogv\", \"wmv2ogv\", \"mp42ogv\", or \"flv2ogv\""  ;;

esac

Last edited by David the H.; 01-17-2009 at 11:11 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash wmv to ogv script True`Colors Programming 5 11-20-2010 11:11 AM
Trying to create a transcoding script (BASH) Phyrexicaid Programming 3 10-15-2007 12:20 PM
Converting video to Ogg / Theora akbar14 Linux - Software 3 11-03-2006 04:38 AM
Converting proprietary audio/video formats to ogg vorbis/theora? brynjarh Linux - Software 2 08-25-2006 11:52 PM
Transcoding mpeg2 video to dv ssfrstlstnm Linux - Software 4 07-12-2005 07:30 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:25 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration