Author Topic: Parsing GVP Files  (Read 2069 times)

0 Members and 1 Guest are viewing this topic.

Offline Joe

  • B&
  • x86
  • Hero Member
  • *****
  • Posts: 10319
  • In Soviet Russia, text read you!
    • View Profile
    • Github
Parsing GVP Files
« on: October 06, 2006, 10:13:21 pm »
GVP (Google Video Player) files are just a nifty list of different tidbits of information about the movie itself, and relatively easy to parse.

Example file:
Code: [Select]
# download the free Google Video Player from http://video.google.com/
gvp_version:1.1
url:http://vp.video.google.com/videodownload?version=0&secureurl=rwAAAIDidGlNAQ_YkX4VMagvL9xntdin--Lm6G32jT3n7q-B4WG0s0gf0f6NLRJOICXKvYb66EF-QWVefbBCg8IHRgxwq26RX160kLvUeZG5btOMywsVpcPYdZ3sCvdmNWmBhOYr60ZrF0mUnUqf6_U3gOBCwsuTDUuzbEjDOMCW70GWrbmm-ubYnPzxqDX1C2BNMZWhZRToUrai53ZVg6ScNjFu5-msgSozlwW_Xq_nhmp3&sigh=lEyTFV-NAsfaXJdnX5EiyazKzOY&begin=0&len=176767&docid=4581508829597815922
docid:4581508829597815922
duration:176767
title:White & Nerdy
description:Music video by "Weird Al" Yankovic from the album "Straight Outta Lynwood"

I wrote a little parser out of boredom for these files and it turned out pretty nice.. *cough* relatively:
Code: [Select]
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
@author Joe[x86]
This class is designed to read GVP files, the files used by google video player
*/
public class GVPReader
{

/**
This is the main method, where error checking is done and stuffs
*/
public static void main(String args[])
{
if(args.length == 0)
{
System.out.println("Usage: java GVPReader filename.gvp");
System.exit(-1);
}

// loop through each file passed to the class
for(int i = 0; i < args.length; i++)
{
parseGvpFile(args[i]);
}
}

/**
This reads the file. Duh!
*/
private static void parseGvpFile(String name)
{
File gvp = new RelativeFile(name);
byte[] data;
try
{
FileInputStream fis = new FileInputStream(gvp);
data = new byte[fis.available()];
fis.read(data, 0, fis.available());
fis = null;

// This SHOULD be outside the try, but "may not be initalized" has kicked my ass -.-
String[] lines = new String(data).split("\n");
for(int i = 0; i < lines.length; i++)
{
parseLine(lines[i]);
}
}
catch(IOException e)
{
System.out.println(e.toString());
System.exit(-1);
}
}

/**
Parses a line, obviously
*/
private static void parseLine(String line)
{
if(line.charAt(0) == '#')
{
return;
}
String[] sections = line.split(":");
// damn not being able to switch on strings -.-
if(sections[0].equals("gvp_version"))
{
System.out.println("GVP Version:\t " + sections[1]);
}
else if(sections[0].equals("url"))
{
// nothing, for now
}
else if(sections[0].equals("docid"))
{
System.out.println("Document ID:\t " + sections[1]);
}
else if(sections[0].equals("duration"))
{
System.out.println("Length (ms):\t " + sections[1]);
}
else if(sections[0].equals("title"))
{
System.out.println("Title:\t\t " + sections[1]);
}
else if(sections[0].equals("description"))
{
System.out.println("Description:\t " + sections[1]);
}
else
{
System.out.println("Unknown Command:\t " + line);
}
}

}





/*
* Created on Dec 4, 2004
* By iago
*/
class RelativeFile extends File
{
public RelativeFile( String file )
{
super( (file.length() > 0 && (file.charAt(0) == '/' || file.matches(".\\:\\\\.*"))) ? (file) : (System.getProperty("user.dir") + "/" + file));
}
}

Example output:
C:\Documents and Settings\Administrator\Desktop>java GVPReader WhiteNerdy.gvp
GVP Version:     1.1
Document ID:     4581508829597815922
Length (ms):     176767
Title:           White & Nerdy
Description:     Music video by "Weird Al" Yankovic from the album "Straight Outta Lynwood"


Thanks to iago for RelativeFile class from javaop2_pub.util. :)
I'd personally do as Joe suggests

You might be right about that, Joe.