Tuesday, January 19, 2010

Code: Serializing and Deserializing Objects (Converting Objects to a String and Back)

Object serialization in .Net is pretty easy, once you have the right code.  It took me a few minutes to figure out the right code so I thought I’d post it here.  The code below is a working C# console app.

Code Snippet
  1. using System;
  2. using System.IO;
  3. using System.Xml.Serialization;
  5. namespace TestApp
  6. {
  7.     public class Program
  8.     {
  9.         static string serializedObject;
  11.         static void Main(string[] args)
  12.         {
  13.             Serialize();
  14.             Deserialize();
  15.         }
  17.         static void Serialize()
  18.         {
  19.             Person p = new Person() { Age = 20, Name = "Bob" };
  20.             XmlSerializer serializer = new XmlSerializer(p.GetType());
  21.             using (StringWriter sw = new StringWriter())
  22.             {
  23.                 serializer.Serialize(sw, p);
  24.                 serializedObject = sw.GetStringBuilder().ToString();
  25.             }
  26.         }
  28.         static void Deserialize()
  29.         {
  30.             XmlSerializer serializer = new XmlSerializer(typeof(Person));
  31.             Person p = (Person)serializer.Deserialize(new StringReader(serializedObject));
  32.             Console.WriteLine("Name:" + p.Name + " Age:" + p.Age);
  33.             Console.ReadLine();
  34.         }
  36.         public class Person
  37.         {
  38.             public int Age { get; set; }
  39.             public string Name { get; set; }
  40.         }
  41.     }
  42. }

Friday, January 15, 2010

Code: Multiple Startup Projects in Visual Studio

Visual Studio has the capability to have multiple projects listed as startup projects.  This will allow you to launch and attach debugging to multiple applications at once.  This is very handy when debugging the encoding system locally.
To do this right click on the Solution icon in Visual Studio and select Properties.
Check the "Multiple Startup Projects" radio button
in the list of projects below the radio button change the Action to "Start" for the projects you want to launch.


Thursday, January 14, 2010

Code: Launch a Console Application in a New Window

I ran into a tricky situation today, How do I use a batch file to launch other batch files or console applications in a new console window?  The key to this is the START keyword.  So if you open up a new command prompt window and enter


You will effectively transfer control of your current window to the test.bat file.  However, if you change your command to

start test.bat

The batch file will be launched in a new window allowing your first window to continue operation to perform other commands or launch more programs.

Wednesday, January 6, 2010

Code: Build an Adaptive Bitrate SMIL File Using XmlDocument

Code Snippet
  1. /// <summary>
  2. /// Builds a smil file based on .mp4 files found in a given directory
  3. /// Assumes files are named XXX_bitrate.mp4 (movie_300.mp4)
  4. /// </summary>
  5. public static XmlDocument BuildSmilFile(DirectoryInfo sourceDirectory)
  6. {
  7.     List<FileInfo> files = new List<FileInfo>();
  8.     files.AddRange(sourceDirectory.GetFiles("*.mp4"));
  10.     //biggest file (highest bitrate) first.
  11.     files.Sort(delegate(FileInfo p1, FileInfo p2)
  12.     {
  13.         return p2.Length.CompareTo(p1.Length);
  14.     });
  16.     XmlDocument result = new XmlDocument();
  18.     //create element nodes
  19.     XmlNode smilNode = result.CreateElement("smil");
  20.     XmlNode headNode = result.CreateElement("head");
  21.     XmlNode bodyNode = result.CreateElement("body");
  22.     XmlNode switchNode = result.CreateElement("switch");
  24.     //add video nodes for each file
  25.     foreach (FileInfo mp4File in files)
  26.     {
  27.         XmlNode videoNode = result.CreateElement("video");
  29.         //Create and add attributes
  30.         XmlAttribute srcAttr = result.CreateAttribute("src");
  31.         srcAttr.Value = mp4File.Name;
  32.         XmlAttribute bitrateAttr = result.CreateAttribute("system-bitrate");
  33.         bitrateAttr.Value = Path.GetFileNameWithoutExtension(mp4File.Name).Substring(mp4File.Name.LastIndexOf("_") + 1) + "000";
  34.         videoNode.Attributes.Append(srcAttr);
  35.         videoNode.Attributes.Append(bitrateAttr);
  37.         //add video node to switch
  38.         switchNode.AppendChild(videoNode);
  39.     }
  41.     //append nodes to parent elements
  42.     bodyNode.AppendChild(switchNode);
  43.     smilNode.AppendChild(headNode);
  44.     smilNode.AppendChild(bodyNode);
  45.     result.AppendChild(smilNode);
  47.     return result;
  48. }

Monday, January 4, 2010

Electronics: Plasma vs. LCD

It seems as if the nation has fallen out of love with plasma televisions, and I can’t figure out why.  Generally speaking plasma’s have better color, higher refresh rates, and deeper blacks than LCD TVs.  Typically the counter argument is that plasma’s use more electricity and their screens are more reflective, which can be a problem in rooms with windows.  Recently I purchased a 42” 720p Panasonic TC-P42X1 plasma to replace my 3 year old 720p 42” Sony Wega LCD projection TV. 

The picture quality of the plasma is outstanding.  One of my biggest complaints with my old Sony was the black levels. This was the main reason I wanted to get a plasma over an LCD, and the Panasonic does not disappoint.  Furthermore it has a 600hz refresh rate so I never notice any ghosting in action movies.  I bought this TV for under $600.  Comparable LCD’s will run you $600-$1000, but at $600 for an LCD you’ll be getting an off-brand with mediocre picture quality.  My friend recently spent $1500 on a 50” 1080p 120hz LCD TV, and I’d take mine over his any day.  Motion images on his 120hz set are so unrealistic that it’s distracting.  Also the color was overly bright, but that was probably more a result of his choice of settings.

The power consumption of TC-P42X1 is also very good.  According to tests done by televisioninfo.com it used between 80 and 200 watts depending on how much white was on the screen at once.  That’s probably the same amount of energy you are using to light your room if you’re not using CFL bulbs.  A similar sized LCD set consumed 137 watts, so I’d actually call the power usage a wash.

Although Panasonic has done work to create anti-glare screens on their plasma’s, I definitely can’t watch the TV with the windows uncovered.  However we do have window blinds in our living room so its a non-issue for me.

Also this television is not 1080p, but I personally think 1080p is overrated.  Right now the only content in 1080p is blu-ray disks, which are expensive, and I don’t have a blu-ray player anyway.  I’ve never watched 720p content and wished for a higher resolution.  I’ll take a 720p TV with great colors and blacks over a poor 1080p TV any day.

So the rest of the world can enjoy spending extra money on LCD and LED sets, I just hope cheap, great plasma’s are still around the next time I need to purchase a TV.