Ubuntu Forums ubuntu.com - launchpad.net - ubuntu help  

Go Back   Ubuntu Forums > The Ubuntu Forum Community > Main Support Categories > Multimedia Production
Register Reset Password Forum Help Forum Council Search Today's Posts Mark Forums Read

Multimedia Production
Discussions about Ubuntu Studio and other multimedia production applications.

 
Thread Tools Display Modes
Old May 29th, 2008   #1
truant
5 Cups of Ubuntu
 
Join Date: Jun 2006
My beans are hidden!
Converting (remuxing) AVC (h264) MKVs to play nicely on a Playstation3

Over the past few months, I've tried many, many different people's scripts/instructions to remux HD video in Matroska containers (.mkv) into a form that the PS3 can play. Most of them work, but none of them are quite easy/friendly enough for me to want to give to my linux-newbie friends.

So, I've knocked up a reasonably friendly script to remux Matroska/AVC video into an mp4 file. It works with just about every source I've tried it with. It attempts to find the real values for things like stream location, frame rate and audio type, but there are dialogs displayed for each so the user can make sure or change if they know better. Just accepting the default values is usually pretty safe.

Because this script remuxes the video rather than re-encodes it, it's very quick, even on slower machines. Bear in mind audio is mixed down to stereo 'cos I don't have an HD sound setup, but if you do, just remove '-ac 2' from the ffmpeg command towards the bottom. You'll probably want to increase the audio bitrate (-ab 256k) at the same time - my TV speakers can't outperform 256Kb audio.

You will need roughly twice the source file's size of disk space free, half of it temporarily.

In addition to standard packages like python, you will require: mkvtoolnix, ffmpeg, mplayer, gpac

You will probably need to compile your own ffmpeg, making sure you include AAC/AC3 audio support. Fortunately, there are plenty of howtos for that out there. All other packages are stock hardy versions.

Also, if anyone wants to make this script better, it's GPL so help yourself. It's not exactly error-friendly, for example.


Copy the below into a new file, make that file executable*, then either run the file or use Nautilus Actions to set it up as a shell extension. Remember to use '%M' as a parameter in Nautilus Actions so the script knows which file you're running it on.

* Right-click the file, Properties, Permissions, then tick "Allow executing file as program" and press 'Close'

Code:
#!/bin/bash

## remuxerator to remux AVC video into PS3-friendly form
# requires: mkvtoolnix (mkvmerge, mkvextract), ffmpeg, mplayer, gpac (MP4Box), zenity

#    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, see <http://www.gnu.org/licenses/>.


# let's have some progress meters.  there's no kill like overkill.
DisplayProgress () {
	inputlength=`mplayer -identify -frames 0 -vc null -vo null -ao null \
               "$INPUT_F" 2>/dev/null | grep ^ID_LENGTH | sed -e 's/^.*=//' -e 's/[.].*//'`
    (
      while ps | grep "$pid " >/dev/null
      do
        secondsCompleted=`tail -c 90 "/tmp/ffmpegalog.log" | awk -F"time=" {'print $2'} | cut -d"." -f 1`
        [ -n "$secondsCompleted" ] || secondsCompleted=0
        percentage=$((100*$secondsCompleted/$inputlength))
        echo "$percentage"
        sleep 1
      done
      echo 100
    ) | zenity --progress --title="Re-encoding audio - ${inputlength}s" \
               --text="" --auto-close --auto-kill --width=500
}

DemuxProgress () {
    (
      while ps | grep "$pid " >/dev/null
      do
	perCentCompleted=`tail -c 90 "/tmp/mkvx.log" | sed 's/.*gress://g' | sed 's/\%//'`
	echo $perCentCompleted
        sleep 0.1
      done
      echo 100
    )  | zenity --progress --title="Demuxing A/V streams" --text="" --auto-close --auto-kill --width=500

}

# filezpls
args=("$@")
if [ -z ${args[0]} ]; then
	INPUT_F=`zenity --file-selection --title="Choose some AVC video to remux"`
else
	INPUT_F=${args[0]}
fi
OUTPUT_F="${INPUT_F}.remuxed.mp4"

# fps pls - this has to be bang-on perfect otherwise a/v will desync.
FPS=`mplayer -identify -frames 0 -vc null -vo null -ao null \
               "$INPUT_F" 2>/dev/null | grep ^ID_VIDEO_FPS | sed -e 's/^.*=//'`
FRAME_RATE=`zenity --entry --text="Choose a frame rate in fps" --entry-text="${FPS}" --title="eff pee ess"`


## remux chain
# and, for a million extra points, add PROPER subtitle support.

# identify A/V streams
STRM_INFO=`mkvmerge -i "$INPUT_F"`
STREAMS=`echo $STRM_INFO | sed 's/File.*ska//' | sed 's/Track ID //g'  | sed 's/(V.*AVC)//'`
V_STREAM=`echo $STREAMS| sed 's/.: audio (.*)//' | sed 's/: video//' | sed 's/^[ \t]*//;s/[ \t]*$//'`
A_INFO=`echo $STREAMS| sed 's/.: video //' | sed 's/: audio//' | sed 's/^[ \t]*//;s/[ \t]*$//'`
A_STREAM=`echo $A_INFO | sed 's/.(.*//' | sed 's/^[ \t]*//;s/[ \t]*$//'`
A_TYPE=`echo $A_INFO | sed 's/^..(A_//' | sed 's/).*//' | awk '{print tolower($1)}' | sed 's/^[ \t]*//;s/[ \t]*$//'`


# show STRM_INFO in a zenity box? "Please select video stream" 
# worth keeping to make sure the above sedwork works
V_STREAM=`zenity --entry --text="${STRM_INFO} 

Please enter which stream number is video" --entry-text="${V_STREAM}" --title="V_STREAM"`

A_STREAM=`zenity --entry --text="${STRM_INFO} 

Please enter which stream number is audio" --entry-text="${A_STREAM}" --title="A_STREAM"`

A_EXT=`zenity --entry --text="${STRM_INFO} 

Please enter audio type file extension (ac3/aac)" --entry-text="${A_TYPE}" --title="A_TYPE"`

zenity --question --text="Press OK to start remuxing, or cancel to stop now." --title="Ready to go"
if [ $? != 0 ]; then
	exit
fi

# extract A/V streams
mkvextract tracks "${INPUT_F}" "${V_STREAM}:${INPUT_F}.tmp.video.h264" "${A_STREAM}:${INPUT_F}.tmp.audio.${A_EXT}" > /tmp/mkvx.log &
pid=$!
DemuxProgress

# convert audio to AAC, downmix to stereo (src usually 5.1)
ffmpeg -i "${INPUT_F}.tmp.audio.${A_EXT}" -acodec libfaac -ac 2 -ab 256k -threads 4 "${INPUT_F}.tmp.audio_final.aac" 2>> /tmp/ffmpegalog.log &
pid=$!
DisplayProgress
zenity --info --text="Audio conversion complete - preparing to mux" &

# change apparent h264 level to 4.1
# replace first "64 00 xx" with "64 00 29" in video file, to fool the PS3.
# mmmm, DIRRTY.
PYPROG="import re; bf = file('${INPUT_F}.tmp.video.h264', \"r+\"); nh = re.sub(\"\x64\x00[\x00-\x99]\", \"\x64\x00\x29\", bf.read(1024)); bf.seek(0); bf.write(nh);"
python -c "${PYPROG}"

# remux into MP4
MP4Box -add "${INPUT_F}.tmp.video.h264" -add "${INPUT_F}.tmp.audio_final.aac" -fps $FPS "$OUTPUT_F"

# tidy up
rm "${INPUT_F}.tmp.video.h264"
rm "${INPUT_F}.tmp.audio.${A_EXT}"
rm "${INPUT_F}.tmp.audio_final.aac"
rm /tmp/ffmpegalog.log
rm /tmp/mkvx.log

# let the user know
zenity --info --text="Remuxing complete"

Last edited by truant; June 5th, 2008 at 11:12 AM..
truant is offline   Reply With Quote
Old June 1st, 2008   #2
fuzzmo
5 Cups of Ubuntu
 
fuzzmo's Avatar
 
Join Date: Aug 2006
Location: London
Beans: 26
Ubuntu 9.04 Jaunty Jackalope
Re: Converting (remuxing) AVC (h264) MKVs to play nicely on a Playstation3

Hi,

many thanks for this except it doesn't seem to work for me - the remuxed file isn't completed and I get these following messages:

Import results: 62870 samples - Slices: 650 I 43115 P 19105 B - 1 SEI - 637 IDR
Stream uses B-slice references - max frame delay 2
Opening file /home/farvesh/downloads/complete/Battlestar Galactica (2003) - 4x08 - Sine Qua Non/battlestar.galactica.s04e08.720p.hdtv.x264-bia.mkv.tmp.audio_final.aac failed
Error importing /home/farvesh/downloads/complete/Battlestar Galactica (2003) - 4x08 - Sine Qua Non/battlestar.galactica.s04e08.720p.hdtv.x264-bia.mkv.tmp.audio_final.aac: Requested URL is not valid or cannot be found
rm: cannot remove `/home/farvesh/downloads/complete/Battlestar Galactica (2003) - 4x08 - Sine Qua Non/battlestar.galactica.s04e08.720p.hdtv.x264-bia.mkv.tmp.audio_final.aac': No such file or directory

I can see the Temp files being created but for some reason it doesn't create the .mp4 file. Any suggestions?
fuzzmo is offline   Reply With Quote
Old June 2nd, 2008   #3
truant
5 Cups of Ubuntu
 
Join Date: Jun 2006
My beans are hidden!
Re: Converting (remuxing) AVC (h264) MKVs to play nicely on a Playstation3

Hmmm. Not sure. told you it wasn't very error-friendly..

does the audio_final file actually exist?

I successfully used this script on the same bsg episode (eg, BiA's rip) as you did, just last week.

Last edited by truant; June 2nd, 2008 at 09:11 AM..
truant is offline   Reply With Quote
Old June 5th, 2008   #4
fuzzmo
5 Cups of Ubuntu
 
fuzzmo's Avatar
 
Join Date: Aug 2006
Location: London
Beans: 26
Ubuntu 9.04 Jaunty Jackalope
Re: Converting (remuxing) AVC (h264) MKVs to play nicely on a Playstation3

Ah right I think I know what the problem is - it creates the *.tmp.ac3 file but doesn't transcode it into *.tmp.audio_final.aac hence where it is failing - I take it I need a compiled version of FFMPEG.... I got my codecs from medibuntu....
fuzzmo is offline   Reply With Quote
Old June 5th, 2008   #5
truant
5 Cups of Ubuntu
 
Join Date: Jun 2006
My beans are hidden!
Re: Converting (remuxing) AVC (h264) MKVs to play nicely on a Playstation3

that sounds about right.

I'm afraid I can't tell you whether the mediabuntu version of ffmpeg has aac support or not (although if you do "ffmpeg -formats|grep aac", you should be able to tell). Libfaac is what you're looking for.

I compiled my own ffmpeg, always have done. I need stuff like libamr and 100% up-to-date mp4 support (not related to this, more for Flash video). Have a go, there's loads of ffmpeg-compiling-howtos out there.

Here's my ./configure command for ffmpeg, in case it's useful:

Code:
./configure --enable-shared --enable-gpl --enable-libvorbis --enable-liba52 --enable-libgsm --disable-debug --enable-libmp3lame --enable-libfaad --enable-libfaac --enable-pthreads --enable-libx264 --enable-libamr-wb --enable-libamr-nb --enable-libxvid --enable-libtheora --enable-nonfree --enable-postproc
truant is offline   Reply With Quote
Old June 5th, 2008   #6
fuzzmo
5 Cups of Ubuntu
 
fuzzmo's Avatar
 
Join Date: Aug 2006
Location: London
Beans: 26
Ubuntu 9.04 Jaunty Jackalope
Re: Converting (remuxing) AVC (h264) MKVs to play nicely on a Playstation3

Ahhh finally sorted it out - uninstalled the medibuntu ffmpeg and installed ffmpeg from this tutorial: http://ubuntuforums.org/showthread.php?t=786095

Works like a charm now - worked fine on my ps3 - many thanks Truant!
fuzzmo is offline   Reply With Quote
Old June 5th, 2008   #7
truant
5 Cups of Ubuntu
 
Join Date: Jun 2006
My beans are hidden!
Re: Converting (remuxing) AVC (h264) MKVs to play nicely on a Playstation3

woop! I made a thing that was useful to someone!

that's made my day, that has. may even warrant a stupid smiley:
truant is offline   Reply With Quote
Old June 5th, 2008   #8
fuzzmo
5 Cups of Ubuntu
 
fuzzmo's Avatar
 
Join Date: Aug 2006
Location: London
Beans: 26
Ubuntu 9.04 Jaunty Jackalope
Re: Converting (remuxing) AVC (h264) MKVs to play nicely on a Playstation3

Lol! Go ahead dude - you deserve it!
fuzzmo is offline   Reply With Quote
Old June 26th, 2008   #9
kingcharles1666
Just Give Me the Beans!
 
kingcharles1666's Avatar
 
Join Date: Nov 2005
Location: Uithoorn, The Netherlands
Beans: 77
Ubuntu 8.04 Hardy Heron
Send a message via ICQ to kingcharles1666 Send a message via Yahoo to kingcharles1666 Send a message via Skype™ to kingcharles1666
Re: Converting (remuxing) AVC (h264) MKVs to play nicely on a Playstation3

Many thanks for the script, works perfect!
kingcharles1666 is offline   Reply With Quote
Old July 27th, 2008   #10
oisuxx
Just Give Me the Beans!
 
Join Date: Nov 2007
Beans: 49
Re: Converting (remuxing) AVC (h264) MKVs to play nicely on a Playstation3

thanks for this script! only problem is now i cant find the file it spits out :\

where is the remuxed file at?
it should be in .h264 correct?
oisuxx is offline   Reply With Quote

Bookmarks

Tags
bash, h264, mkv, mp4, playstation, remuxing

Thread Tools
Display Modes

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 On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 10:38 AM.


vBulletin ©2000 - 2010, Jelsoft Enterprises Ltd. Ubuntu Logo, Ubuntu and Canonical © Canonical Ltd. Tango Icons © Tango Desktop Project. lingonberry