Outline: The goal for this problem is to create a command line program that will take a subtitle file, specifically an .srt file, and shift the timings either forwards or backwards by any amount of time. The example command line operation given is:
shift_subtitle --operation add --time 02,110 input_file output_file
My initial thoughts are not so much about how the algorithm will work, but on how to create a command line operation. I would normally just have a script do_something.rb and then just run it with the interpreter, ruby do_something.rb parameters.
Well, as it turns out, making a command line application in Ruby is pretty straightforward. All you really need to do is include the line #!/usr/bin/env ruby at the top of your script and change the permissions. I can’t seem to make it run properly without the ./ in front of it, but that seems like a minor thing that I’ll figure out later. I’d really like to start with the code.
Since this problem is pretty well presented I know what the options are going to be so I’ll start by looking at how Ruby handles arguments and options first since I’d like to get that out of the way. It looks like I’ll be needing the ‘optparse’ class from the Ruby standard library.
This is the initial code I just wrote up (essentially copy/paste) to parse options and flags. I’ll be using charpcfn.pastebin.com for larger snippets of code, but eventually I’d like to integrate them into the blog.. http://charpcfn.pastebin.com/d3b3fe761
So it looks like I’ll be doing this in parts. I feel like this is a good start to the program, get the basic stuff out of the way and think about the algorithm for a while. I’ll probably be back soon, learning a new language is pretty fun.
edit: One should fully read the problem before starting. ‘optparse’ was mentioned, and in fact required for the actual problem, and I didn’t even realize. Always read the whole problem before starting :]Advertisements