Tuesday, May 10, 2011

A Youtube Uploader - the UNIX Way!

My university's pantomime society has recently started putting a whole lot of clips of its past shows up on youTube. And since I'm the resident "computer guy", I've been given the job of transcoding/uploading them all.

Transcoding was easy enough - just install ffmpeg, write a quick bash script, and we're done. On the other hand, there didn't seem to be an obvious way to automate the upload process.

What I really wanted was a program I could use from the terminal, something I could script. Ideally, I should also be able to schedule the uploads so that they ran during the middle of the night (when I'm not using the computer and my ISP doesn't monitor bandwidth usage). Googling didn't turn up any suitable candidates - there were a few youtube upload applications, but they were all windows only. And while I do still have a windows installation on my laptop, I didn't really feel like booting a different OS just to upload a few videos. I wanted something that worked on Linux.

So I did what any good hacker would do - I've written up a quick python script to do the job. I've tried to follow the UNIX design philosophy in writing the program. Thus it doesn't transcode video. It doesn't have a gui. The scheduling is handled using at. In summary, it does just one thing (uploads videos to youtube) and tries to do it well. But the end result is a program which can be combined with others to offer a lot of flexibility, and is easy to automate using scripting.

You can get the script here. You will need to download and install Google's gdata library for python, which you can get here. You can either elect to install the library as per the installation instructions, or else you can simply extract the contents of the archive into the same directory as the uploader script. To run the script, you will need python 2.6. The reason for this is that I'm starting to get into the habit of using the print function (instead of print statement) in python 2.6 to get ready for eventually moving to python 3. However, as far as I'm aware, the gdata library only supports python 2.x. Thus the only python version capable of running the script is python 2.6. If this is a problem, the print functions could be replaced with print statements, allowing the script to run on python 2.x (with x < 6). This is left as an exercise for the reader (which is of course code for "I'm too lazy to do it, if it's a problem do it yourself" :P ).

Finally, you will also need a YouTube API key. You can generate a key here. You will need to add this key to the uploader script. Open the file "youtubeUploader.py", and replace the text "YOUR API KEY HERE" (near the top of the file) with your newly generated API key.

There's still plenty of room for improvement - for instance it would be nice to have some indication of the upload progress while files are uploading. I might add this feature at some point, although I couldn't work out how to do it from the gdata documentation. Anyway, hope someone finds the script useful (if only as a demo of how to use the gdata api).

Monday, May 9, 2011

Poll Mod v0.1.1

This is a minor update to the 0.1.0 version of my poll mod for punBB 1.2. It contains a single bugfix. Previously, if anyone had a user name containing the character "@", they were allowed to vote multiple times, but not allowed to see the poll results. This bug has now been closed.Link

robs_poll_mod_0.1.1

Saturday, May 7, 2011

A quick script for concatenating MTS files

A small script I wrote to fix some problematic video files. In this case the problems were due to the design of the video camera I was using. Basically after recording 2GB of data, the camera was dumping whatever data it had recorded into an mts file, and then starting a new recording. While all the camera data ended up being recorded, problems arose with the second file, as the keyframe information was in the wrong place, etc. Basically, whoever designed the camera (some team at Sony) were morons.

Here's a simple script I wrote to fix their incompetence. Usage is mtscat -o output_file file1.mts file2.mts ...
Essentially does the same thing as "cat file1 file2 file3... > output_file" except that for the second and subsequent files it skips over the first 192 bytes (the header for the MTS stream)

Script is linked below:
mtscat.py