Archive

Posts Tagged ‘bash’

Turning mkv into mp4 for the PS3 [UPDATED]

February 21st, 2010 Fighter Hayabusa 1 comment

Quite a lot of acronyms there don’t ya think? ;-)

Anyway… The PS3 is fantastic. I think most people with half a brain will agree on that. It does however have it’s shortcomings and one of them is that it doesn’t play mkv-files. So whenever I stumble upon something in that format I have to perform some sort of black magic on it in order to turn it into an mp4 that the PS3 agrees with.

The easy way to go is to just flush it through HandBrake. That works but then it will be decoded and re-encoded which potentially causes quality degradation and takes foreeeeeever. So that’s not really an option I’m happy with.

However, decoding and re-encoding is actually unnecessary since what’s wrong is the container and not the contents. Luckily unpacking and repackaging the mkv as mp4 can all be done with a bunch of free and open source tools. It’s still a bit of a hassle though so today I spent a couple of hours putting together a script that makes it semi-automatic and a lot easier.

One thing that proved to be a bit more work than I expected was turning DTS 5.1 audio into AAC 5.1. What I came up with should work, but I haven’t been able to verify it since my receiver doesn’t support AAC 5.1. If yours does, please let me know if it works or not. For this reason in my personal version of the script I turn DTS 5.1 into AAC stereo instead (using the last part of the if-elif-else statement), which is OK even if it’s not quite as nice as 5.1 sound would have been.

All of the tools I used are open source software and available for most Linux-flavors (I use Fedora 12), except Nero AAC Encoder which is just free as in beer.

So that nobody else has to suffer through the process of putting a script like this together; here’s the script. It’s fairly self-explanatory and I even commented the code a bit so it shouldn’t be that hard to understand what it does and why. Enjoy!

Download the script here!

While putting together the script I found this article very useful for deciphering the mess that is audio codecs. I would also like to give a shout-out to one of my co-workers who introduced me to several of the tools and how they work.

UPDATE:
In further experimenting I found that my original script contained a couple of bugs. I believe I’ve sorted those out now and I’ve updated the code above. I’m still not claiming this script to be bug-free so if you find anything that’s not working, please let me know.

Finding what’s after a specific string in a text-file

November 7th, 2009 Fighter Hayabusa No comments

Have you ever wanted to find a specific string in a log-file and then output what comes after that line in the file? I do this every single day at work and can’t be the only one. Somewhere in a huge log-file, XML-file or other type of text-file there’s a line that equals the beginning of some specific type of information that I’m in need of so I need to find that line of text and output a bunch of the lines following it.

I might for example have a backup cron-job that when it starts outputs “Backup started” to /var/log/messages and then follows that with output concerning how the backup went. Sure, theoretically I could open the file in vim and simply search for the line in question but sometimes text-files are frikkin’ huge and opening them up in a text-editor is just not a viable option. For example, I handle XML-files that are several gigabytes in size daily and loading one of those into an editor will mean nothing but pain and suffering I assure you ;-)

So to facilitate this procedure I slapped together this script, which I’m giving the easy-to-remember name fsagsl.sh:

#!/bin/bash
# fsagsl.sh Find String And Grab Some Lines
# 2009 (c) IDontGiveASmegWhatYouDoWithIt License
# by FighterHayabusa <fighterhayabusa@barbedwirebytecodebaconburger.com>
# Finds a string in a text-file and then outputs
# a chosen number of lines from that position onwards.

function is_int()
{
[ "$1" -eq "$1" ] > /dev/null 2>&1
return $?
}

PRG=fsagsl.sh

if [ -f $3 ] && is_int $2; then
LINESTART=`grep -n $1 $3 | sed -e 's/\([0-9]*\):.*/\1/g'`
for L in $LINESTART; do
let LINESTOP=$L+$2
sed -n "${L},${LINESTOP}p" $3
done
exit 0
else
echo "ERROR: Invalid parameters."
echo "Usage: ./$PRG string_to_find number_of_lines filename"
echo "Example: ./$PRG \"Backup started\" 5 /var/log/messages"
exit 1
fi

As you may notice the script also has some simple error-handling and  handles if there are multiple instances of the string that is being looked for.

So there you go. I hope somebody finds it useful :-)