Transcoding into a 3gp Video

From MythTV Official Wiki
Revision as of 23:23, 27 December 2007 by TwoOneSix (talk | contribs) (added section to remove commercials before encoding)

Jump to: navigation, search

Introduction

These quick notes detail the steps I took to create 3gp videos from my recordings taken from MythTV. You will probably want to tune the various parameters.


How To:

Encode with commercials

Simply create a script with the following code in it:

nano /home/mythtv/3gp.sh
#!/bin/sh
# MythTV user job to transcode a video to 3gp suitable for a phone
FILE=$1
TITLE=$2
OUTDIR="/tmp/"
/usr/bin/ffmpeg -i $FILE -ar 16000 -ac 1 -acodec libamr_wb -ab 23.85k -b 240 -s qcif -f 3gp "$OUTDIR$TITLE.3gp"

# Uncomment the following line for a LG VX9900 (enV) video
#/usr/bin/ffmpeg -i $FILE -ar 16000 -ac 1 -acodec aac -ar 44100 -ab 128 -b 240 -s qcif -f 3gp "$OUTDIR$TITLE.3gp"

Make the script executabled:

chmod u+x /home/mythtv/3gp.sh

To use the script simply call it like:

/home/mythtv/3gp.sh /myth/1004_20070912180000.mpg "The Simpsons - Marge Be Not Proud"

Note: The title is in quotes, to ensure it is passed to the script as $2, without the quotes $2 would be "The", $3 "Simpsons" etc....

Encode without commercials

With this option we have a script that you can use from one of your User Jobs or fire it off from the command line... your choice. Either way encoding without commercials can save you a lot of precious space on your mobile device. I have chosen (via the below ffmpeg settings) to trade some of that space savings for better quality video, you decide what's best for your setup.

Just create the following file, make it executable and setup your user job of hit the command line.

Important.png Note: This script is a slightly modified version of source at Script - RemoveCommercials.

Script.png /usr/bin/myth_3gp

#!/bin/sh
VIDEOIN=$1
FILENAME=$2
OUTDIR=$3
# Sanity checking, to make sure everything is in order.
if [ -z "$VIDEOIN" -o -z "$FILENAME.3gp" ]; then
        echo "Usage: $0 <PathToVideoFileName> <OutFile> <OutDirectory>"
        exit 5
fi
if [ ! -f "$VIDEOIN" ]; then
        echo "File does not exist: $VIDEOIN"
        exit 6
fi
# The meat of the script. Flag commercials, copy the flagged commercials to
# the cutlist, and transcode the video to remove the commercials from the file.
# Creating cutlist
mythcommflag --gencutlist -f $VIDEOIN
ERROR=$?
if [ $ERROR -ne 0 ]; then
        echo "Copying cutlist failed for ${FILENAME} with error $ERROR"
        exit $ERROR
fi
# Exporting video minus the data in the cutlist
CUTLIST=$(mythcommflag --getcutlist -f $VIDEOIN | tail -n 1 | awk '{print $2}' | sed 's/,/ /g')
ERROR=$?
if [ $ERROR -ne 0 ]; then
        echo "Copying cutlist failed for ${FILENAME} ($VIDEOIN) with error $ERROR"
        exit $ERROR
fi
mythtranscode -m -i $VIDEOIN --honorcutlist "$CUTLIST" --showprogress -o "$OUTDIR/$FILENAME.tmp"
ERROR=$?
if [ $ERROR -ne 0 ]; then
        echo "Transcoding failed for ${FILENAME} ($VIDEOIN) with error $ERROR"
        exit $ERROR
fi
# Removing the cutlist from recording (but leaving commercial flags)
mythcommflag --clearcutlist -f $VIDEOIN
ERROR=$?
if [ $ERROR -ne 0 ]; then
        echo "Could not clear cutlist. Aborted with error $ERROR"
        exit $ERROR
fi
# Now we'll convert the new temp file to 3gp using ffmpeg
/usr/bin/ffmpeg -i "$OUTDIR/$FILENAME.tmp" -ac 2 -acodec aac -ar 32000 -qscale 3 -r 15 -s qcif -title "$FILENAME" -f 3gp "$OUTDIR/$FILENAME.3gp"
ERROR=$?
if [ $ERROR -ne 0 ]; then
        echo "Could not covert file to 3gp. Aborted with error $ERROR"
fi
# Now, regardless of an ffmpeg error we'll remove the temp file
rm -f "$OUTDIR/$FILENAME.tmp"
rm -f "$OUTDIR/$FILENAME.tmp.map"

Important.png Note: You're going to want to change some of the ffmpeg commands as the above encodes for my enV phone which can handle high framerates and stereo (2 channel) audio. Also, the above script yields a roughly 70MB file for every 20 minutes of video (30 minute program minus 10 minutes of commercials).

Now, make that file executable:

#chmod a+x /usr/bin/myth_3gp

Running from a frontend

Do the following on the backend...

  1. Run “mythtvsetup”
  2. Select “General”
  3. Next until the “Job Queue (Host-Specific)” screen
  4. Place a check on “Allow User Job #1” (or whatever job number your using)
  5. Next until the “Job Queue (Job Commands)" screen and enter the following:
  6. User Job #1 Description: Convert for phone (mine is called Convert for enV)
  7. User Job #1 Command: /usr/bin/myth_3gp "%DIR%/%FILE%" "%TITLE%-%SUBTITLE%" "<the_directory_where_you_want_to_export_the_video>"
  8. Now, exit mythtvsetup

Lets test things out on the frontend...

  1. Go into Media Library -> Watch Recordings
  2. Select a recording, hit the right arrow, and select Job Options
  3. You should see a new job called “Begin Convert for phone” choose it

Running from command line

You can also fire the script from the command line, but it can be a PITA to find the file you want to encode... at any rate, you do it like this:

$/usr/bin/myth_3gp /myth/tv/1004_20070912180000.mpg "The Simpsons - Marge Be Not Proud" "/myth/shared/video/"

Script overview

Now what's going to happen is the script will create a cutlist based on the commercial flagging or I think it will use an existing cutlist in case you want to create one yourself, but be warned that unless you comment out the line in the script above, it will automatically clear the cutlist when it's done. Then, it's going to generate a couple of files (a .tmp and a .key file) while it's building the .3gp file. Once the .tmp and .key file are removed and the job says "Completed" in the log it means the script it done and your file is ready for your mobile device.

Automatically Transcoding

To automate this process simply create a User Jobs to call the script, my UserJob1 is as follows:

/home/mythtv/3gp.sh "%DIR%/%FILE%" "%TITLE% - %SUBTITLE%"


Variables

There are many variables you may want to change within the script:

Options for User Jobs on the Backend
Option What it does Possible Settings
-acodec Audio Codec libamr_wb, libamr_nb (depending on your version of ffmpeg you may need to drop the lib, ie amr_wb ro amr_nb.)
-ab Audio Bit Rate 6.6k, 8.85k, 12.65k, 14.25k, 15.85k, 18.25k, 19.85k, 23.05k, or 23.85k (for libamr_wb)
-b Video Bit Rate any value you choose
There are many other options, see
man ffmpeg