MediaSuite.NET Sample code

How to extract information from a FLV file

This sample demonstrates the use of FLVReader and how to extract information from the script tag contained in Flash Video Files by iterating over ObjectDataReference structures.

using System;
using System.Collections.Generic;
using StreamCoders.Reader;
 
namespace FLVInfo
{
    class Program
    {
        static bool Dispatch(string spacer, ObjectDataReference tag)
        {
            if (tag.DataType == typeof(List<ObjectDataReference>))
            {
                PrintList(spacer + ".", tag.DataValue as List<ObjectDataReference>);
                return true;
            }
 
            if (tag.DataType == typeof(Dictionary<string, ObjectDataReference>))
            {
                PrintDictionary(spacer + ".", tag.DataValue as Dictionary<string, ObjectDataReference>);
                return true;
            }
            return false;
        }
 
        static void PrintDictionary(string spacer, Dictionary<string, ObjectDataReference> tag)
        {
            if (tag == null) return;
 
            foreach (KeyValuePair<string, ObjectDataReference> kvp in tag)
            {
                if (Dispatch(spacer, kvp.Value) == false)
                    Console.WriteLine("{0}{1} : {2}", spacer, kvp.Key, kvp.Value.DataValue);
            }
        }
        static void PrintList(string spacer, List<ObjectDataReference> tag)
        {
            if (tag == null) return;
 
            foreach (ObjectDataReference odr in tag)
            {
                if (Dispatch(spacer, odr) == false)
                    Console.WriteLine("{0}{1}", spacer, odr.DataValue);
            }
 
        }
        static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                Console.WriteLine("FLVInfo filename");
                return;
            }
            FLVReader reader = new FLVReader();
            reader.FileName = args[0];
            bool res = reader.Init();
            if (res == false)
            {
                Console.WriteLine("Unable to parse flv file");
                return;
            }
            if (reader.ScriptDataTag.ObjectValues.Count > 0)
            {
                foreach (KeyValuePair<string, ObjectDataReference> kvp in reader.ScriptDataTag.ObjectValues)
                {
                    if (kvp.Value.DataType == typeof(List<ObjectDataReference>))
                        PrintList("", kvp.Value.DataValue as List<ObjectDataReference>);
                    else
                        Console.WriteLine("{0} : {1}", kvp.Key, kvp.Value.DataValue);
                }
            }
            else
                Console.WriteLine("No tags present.");
        }
    }
}