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).

No comments:

Post a Comment