Tag Archive for 'tutorial'

DVD Ripping the Windows Way

I finally decided to digitize my dvd collection.  Two things prompted this, 1 the rack is running out of room (see below) and 2 they are all at my parents house.

dvdrack

This prompted research as how to break the annoying dvd decryption and obtain personal use backups of dvd’s I actually legally purchased (as opposed to many of my other collections).   Googling quickly showed a few programs that work and I find useful.  The first being the zombie program DVD Shrink.

While DVD Shrink is no longer being worked on,  you can still find it floating around the net.  The most recent version is 3.2.0.15, note it is freeware so avoid the sites that charge for it.  Or just download it here.

The next piece of software used is called Handbrake, which takes dvd formatted data and converts it to normal video files such as mp4, avi or ogm.  It is open source and therefore free.  The thing I am liking most about it is the ability to queue up multiple conversions and let it run overnight, which is important as it takes around 1.5 hours on my gaming machine per movie.

The two afore mentioned pieces of software work most of the time but since DVD Shrink is a few years old it can’t break some of the newest encryption methods (damn you Disney).  This is annoying because Wall-E is an awesome movie.  So to get around this I found AnyDVD HD.  It tears out the encryption on all dvd’s, the HD version even works on blue-ray and HD discs which is awesome.  The only not awesome part is the price, 79 euros = $104 real dollars.  There is no way I was dropping that kind of money on software for a single week long ripping party, luckily they have a 3 week trial version.  The only downside is it forgets your settings when it shuts down, which only matters if you turn off your computer (I don’t).

With my newly assembled arsenel of software tools I began tearing into the rack of disc’s like a rabit badger, or at least a wounded chipmunk.  Here is my method.

DVD Shrink makes fine backups but as they are huge (4+ gigs each, some over 8 ) I decided to convert them to the more portable and popular avi format.  Max quality avi’s of these work out to be around 1.6 gigs tops (Lord of the Rings), pretty close to 1.4 gigs on average though.  If space is an issue then they can be shrunk later on.  Let’s start making the MPAA cry.

First you insert the dvd in your machine and launch DVD Shrink.  Then choose open disk.  If you have it AnyDVD should be launched first as it intercepts dvds before you open them in DVD Shrink.  Closing or modifying AnyDVD settings while ripping with DVD Shrink causes it to lose connection to the disk and you have to start over.  When the fox icon is pink it is thinking, when it is red it is ready.

rip1

Wait while it analyzes the movie, this takes about 2 minutes.  Usually mine start off at a pretty slow speed, like 2,000 kbps then work up to 9,000 max.  The encoding phase does this as well.

rip2

If you want to back up the entire disk, menus extras etc, then just choose backup after the dvd is analyzed and skip the next step.  If like me you are only interested in the feature film go to re-author and drag the title from the “Main Movie” area over to the empty area on the left.  Now when you choose backup it will only save that one, cutting down on important disk space.  I have a 1 terabyte drive as the buffer for this but 7 gigs adds up pretty quickly.

rip3

Once you have the tracks to rip, let ‘er rip.  It takes anywhere from 7 minutes to an hour, depending on how much there is to work with and if you chose any other fancier options.  Generally get less than 10 minutes though.  Again it starts off slow and works up to a decent speed.

rip5

This is the message you are looking for, the a-ok to move on.  Congratulations, you are now out a large chunk of disk space.  If you are ripping multiple disks at once make you to switch back to “Full Disc” mode as otherwise it pops up an annoying message and beeps at you.  Also you can’t open a new disc until you change to full disc mode.  Note the discrepency in times between the previous picture and this one.

rip7

Next we can start converting the big honking mess of dot vob files into a usable format.   Start up Handbrake and choose Source in the upper left corner, then select the DVD / VIDEO_TS Folder.  Find the folder you want to convert on your hard drive and select it.  Sometimes you have to choose the VIDEO_TS folder itself, the immediate parent one won’t work.

rip8

Then you choose the file name, type and all the other settings I don’t mess with to create the actual file you wanted in the first place.  Normal has worked in all cases, I just change the file type.  Make sure to look at the title drop down and check that the file length is appropriate (seen here), some of them have multiple ones to screw you up.  Annoying to waste 2 hours to find out you chose the wrong chapter to burn.

rip9

Choose where you want to save the file and what to call it.  Also the file type, I went with avi as it is widely supported and ogm didn’t work on my machine (will figure that one out later).

rip11

Go over all the settings to make sure it looks good then start it going.  The start button makes it go right now, alternatively you can hit the “Add to Queue” and make a big list of them for your computer to work on while you sleep.  It is very cpu intensive so best to let it run alone on your machine if at all possible.  When you hit Start you get a friendly CMD.exe window that has an eta on it.  Don’t close this window.  It works in two stages, the first is encoding which is the fast phase.

rip12

Then there more encoding which eventually writes it to disk.  Takes over an hour for my machine per dvd so settle in.  This next picture is just to show what the second phase looks like.

rip13

The next to final stage is to verify the rip actually worked.  Choose your favorite media program (I highly recommend VLC) and load the file, make sure the sound is sync’d right and other such things.  Note the video did work, the screenshot capture method didn’t.

rip14

When you know the video ripped correctly go back and delete the raw dvd folder as it is just a waste of space.  Now you too in around 2 hours can make digital copies of stuff you already owned.  Sad fact of the day: with a fast internet connection it is  quicker to pirate the movies than convert the ones you own.  But piracy is wrong, and backing up dvds is mostly legal.

One final tip, you can run as many copies of DVD Shrink as you have dvd drives.  I crank on two at a time, tried to put in a third pulled from an old computer but my motherboard didn’t have a second ide cable.

rip151

Have fun backing up you movies!

Boost Asio Serial_Port Demo

So I’ve recently started playing with hardware more.  This necessitated learning how to communicate my C++ with the actual hardware.  The de facto method for this for decades has been the RS232 serial port.  But how to actually use this to talk with code?

In Unix you can directly talk to dev/ttyS0 or wherever the device is located.  Windows gives you some com stuff to talk to it.  But is there a way to do it cross platform?

Of course there is, otherwise I wouldn’t write about it.  Enter Boost, the pivotal C++ library.  In Boost::Asio (ASynchronous Input/Output) there exists a serial_port class.  Seems to be good enough, all that is left is to get it to work.  Easier said than done.  There are basically no examples and the doc’s aren’t very helpful (weren’t to me at least).

So here is a small program that covers most of what a serial_port class needs to do.  You set the parameters before the program starts (certain os’s have limits built in, like you must set the baud rate beforehand etc).  The hardware this talks to takes a baud rate of 19200, 8 bits of data, no parity, and 1 stop bit.  My test board only read input so the example doesn’t take input yet.  Check the comments for more details, the license is at the top of the file.

Due to the stupidity of Wordpress the code format kept getting eaten so you have to download the file instead.

Link to file: boost_serial_port_demo.cpp

Big O Notation

It is time for a primer on big o.
Big O

No, not that Big O. I am talking about the one we use in computer science. (If you don’t undestand the picture it is from an anime called The Big O)

What is big o notation?

Big O notation is a method used in computer science, and mathematics, to describe the efficiency of an algorithm. There are several other notations but big o is the most common for computer science. In short it is a function that describes the worst case performance of an algorithm. Big o means given n inputs the algorithm uses O(x), pronounced O of x, resources. x is a function that depends on n. Generally we are measuring operations, and thus speed, but it can also be used for memory usage or many other metrics.  Big o is useful because it tells us the algorithm will never be any worse than this. Little o tells you it can never be better than this and theta notation says it will be in this family, but those are beyond the scope of this post.

Figuring out big o

Actually proving some algorithm belongs to a certain set of big o families is not always easy but it is usually pretty simple to ballpark it. There are several commonly used types that we will cover.

Constant Time

Let’s look at a simple function, it just returns a value from an array given the index.  All the examples are going to be in C++, just a forewarning.

int getIndex( int Data[], int Index )
{
   return Data[Index];
}

This will take the same amount of time no matter what values we give it. We call this constant time or O(1). Now for a more interesting example.

Logarithmic Time

Logarithmic time is most often found where you partition the data into two even parts then continue working on one of them.  Binary search is a good example of this, it looks for a value in a sorted array.  It chooses a pivot value in the middle, determines if the value it is looking for is above or below the pivot then starts the search again using the new partition.

int binarySearch(int sortedArray[], int first, int last, int key) {
   while (first <= last) {
      int mid = (first + last) / 2; // compute mid point.
      if (key > sortedArray[mid])
         first = mid + 1; // repeat search in top half.
      else if (key < sortedArray[mid])
         last = mid – 1; // repeat search in bottom half.
      else
         return mid; // found it. return position /////
   }
return -(first + 1); // failed to find key
}

The proof showing that the relation between amount of work down and the number of inputs is not trivial so I will just say that these types of algorithms are classified as O(log n).  (Look up master theorem if you need more nitty gritty)  Technically it should be log base 2 n but few people complain if you just say log n.

Linear Time

Here is a function that takes an array of ints and prints them out.

void printInts( int Data[], int Size )
{
   for( int i = 0; i < Size; i++ )
   {
      std::cout << Data[i] << std::endl;
   }
}

We would say that Size is n because that is how many elements we have to look at. Since there is no way to get the job done without printing each of the n elements this is O(n) or linear time.

Polynomial

If you take a loop that is called n times and place a loop that does an additional n work what do you get? You get polynomial time.

void printAll( int Data[][], int SizeOne, int SizeTwo )
{
   for( int i = 0; i < SizeOne; i++ )
   {
      for( int j = 0; j < SizeTwo; j++ )
      {
         std::cout << Data[i][j] << ” “;
      }
      std::cout << std::endl;
   }
}

If we say SizeOne is n and SizeTwo is m, then we do n work, but each n work consists of m work as well. So we have to do n*m work. If n = m we would get n*n or n^2 work. Polynomial time is anything of the form n^m, where m doesn’t have to be an integer but it is greater than one. O(n^2) is referred to as quadratic because it appears often enough to get a special name.  This is written as O(n^2), O(n^3), etc.

Others

Exponential is O(c^n), where c is some some constant value. Examples that use this would be the traveling salesman problem, a notoriously hard problem in computing.

N to the n is O(n^n), something you should avoid writing if at all possible. These are slow. And by slow I mean the universe will end before this code finishes running.

In summary here are the most common forms and their names.
Big O Notation table

Why You Care

If each operation takes one second, then with n at 1 million this is how long it takes for each of the common ones to run.

Big O Timing

It should be obvious from the chart that a small change in the big o value of your algorithm can make a huge difference in computation time.  For most problems you can find something that is polynomial or better, log-linear being the best you can get for many problems.

Big o is also the worst case performance.  So getting O(n^2) may only occur when all the ducks are in a row, average case may be O(n log n) which is much faster for large n.  Another thing to consider is while many algorithms may have the same big o implementations affect performance as well.  So for small data sets a lean O(n^2) algorithm may out perform a O(n log n) one that has large amounts of overhead.  All things to keep in mind when you are writing code.