Saturday, 5 January 2013

MediaInfo Wrapper for .NET Projects

Latest version: v1.0.0.0 (2013-01-05)
Project Page: SourceForge.net Google Project Hosting
License: Open-Source (GNU General Public License v2)

About

MediaInfoNET is a .NET wrapper for the MediaInfo library.
MediaInfoNET.dll can be added to any VB or C# project and used for reading information from audio and video files.

Downloads

MediaInfoNET Binaries (835 KB)
MediaInfoNET Source (15 KB)

Usage

  • Add a reference to MediaInfoNET.dll to your project and import the MediaInfoNET namespace.

VB:

Imports MediaInfoNET

CS:

using MediaInfoNET;
  • Create an object of the MediaFile class and pass the file path as a parameter. The file properties can be read from the MediaFile object.

VB:

Dim aviFile As MediaFile = New MediaFile("C:\Sample.avi")

CS:

MediaFile aviFile = new MediaFile("C:\Sample.avi");
  • Download the latest copy of MediaInfo.dll from the MediaInfo website (32-bit DLL without installer) or use the file that is included with the MediaInfoNET download package. Place MediaInfo.dll in the application directory.

Complete Examples:

VB:

Imports MediaInfoNET

Module Module1

    Sub Main()
        Dim aviFile As MediaFile = New MediaFile("C:\Sample.avi")

        Console.WriteLine()
        Console.WriteLine("General ---------------------------------")
        Console.WriteLine()
        Console.WriteLine("File Name   : {0}", aviFile.Name)
        Console.WriteLine("Format      : {0}", aviFile.General.Format)
        Console.WriteLine("Duration    : {0}", aviFile.General.DurationString)
        Console.WriteLine("Bitrate     : {0}", aviFile.General.Bitrate)

        If aviFile.Audio.Count > 0 Then
            Console.WriteLine()
            Console.WriteLine("Audio ---------------------------------")
            Console.WriteLine()
            Console.WriteLine("Format      : {0}", aviFile.Audio(0).Format)
            Console.WriteLine("Bitrate     : {0}", aviFile.Audio(0).Bitrate.ToString())
            Console.WriteLine("Channels    : {0}", aviFile.Audio(0).Channels.ToString())
            Console.WriteLine("Sampling    : {0}", aviFile.Audio(0).SamplingRate.ToString())
        End If

        If aviFile.Video.Count > 0 Then
            Console.WriteLine()
            Console.WriteLine("Video ---------------------------------")
            Console.WriteLine()
            Console.WriteLine("Format      : {0}", aviFile.Video(0).Format)
            Console.WriteLine("Bit rate    : {0}", aviFile.Video(0).Bitrate.ToString())
            Console.WriteLine("Frame rate  : {0}", aviFile.Video(0).FrameRate.ToString())
            Console.WriteLine("Frame size  : {0}", aviFile.Video(0).FrameSize.ToString())
        End If

        Console.ReadLine()
    End Sub
End Module

CS:

using System;
using System.Text;
using MediaInfoNET;

namespace SampleCS
{
    class Program
    {
        static void Main(string[] args)
        {
            MediaFile aviFile = new MediaFile("C:\Sample.avi");

            Console.WriteLine();
            Console.WriteLine("General ---------------------------------");
            Console.WriteLine();
            Console.WriteLine("File Name   : {0}", aviFile.Name);
            Console.WriteLine("Format      : {0}", aviFile.General.Format);
            Console.WriteLine("Duration    : {0}", aviFile.General.DurationString);
            Console.WriteLine("Bitrate     : {0}", aviFile.General.Bitrate);

           if (aviFile.Audio.Count > 0)
           {
                Console.WriteLine();
                Console.WriteLine("Audio ---------------------------------");
                Console.WriteLine();
                Console.WriteLine("Format      : {0}", aviFile.Audio[0].Format);
                Console.WriteLine("Bitrate     : {0}", aviFile.Audio[0].Bitrate.ToString());
                Console.WriteLine("Channels    : {0}", aviFile.Audio[0].Channels.ToString());
                Console.WriteLine("Sampling    : {0}", aviFile.Audio[0].SamplingRate.ToString());
           }

            if (aviFile.Video.Count > 0)
            {
                Console.WriteLine();
                Console.WriteLine("Video ---------------------------------");
                Console.WriteLine();
                Console.WriteLine("Format      : {0}", aviFile.Video[0].Format);
                Console.WriteLine("Bit rate    : {0}", aviFile.Video[0].Bitrate.ToString());
                Console.WriteLine("Frame rate  : {0}", aviFile.Video[0].FrameRate.ToString());
                Console.WriteLine("Frame size  : {0}", aviFile.Video[0].FrameSize.ToString());
            }

            Console.ReadLine();
        }
    }
}

43 comments:

  1. Do you know how to make it work in windows 8 app?
    MediaFile is not getting the files properties.

    ReplyDelete
    Replies
    1. I haven't tried it on Win8..will check

      Delete
  2. Great Job. I enjoyed using it. Thanks!

    ReplyDelete
  3. Hi,
    When I run the test program that included MediaInfoNET, I got the error below (I tried compiled them in net 4.5, C# - same problem).
    Thanks


    System.BadImageFormatException was unhandled
    HResult=-2147024885
    Message=An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)
    Source=MediaInfoNET
    StackTrace:
    at MediaInfoNET.MediaInfo.MediaInfo_New()
    at MediaInfoNET.MediaInfo..ctor() in O:\CodeMagic\MediaInfo\MediaInfoNET_Source\MediaInfoDLL.vb:line 77
    at MediaInfoNET.MediaFile.GetMediaInfo(Boolean AppendInfo) in O:\CodeMagic\MediaInfo\MediaInfoNET_Source\MediaFile.vb:line 82
    at MediaInfoNET.MediaFile..ctor(String SourceFile) in O:\CodeMagic\MediaInfo\MediaInfoNET_Source\MediaFile.vb:line 62
    at WindowsFormsApplication1.Form1.button1_Click(Object sender, EventArgs e) in o:\Housekeeper\test3\WindowsFormsApplication1\Form1.cs:line 45
    at System.Windows.Forms.Control.OnClick(EventArgs e)
    at System.Windows.Forms.Button.OnClick(EventArgs e)
    at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
    at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
    at System.Windows.Forms.Control.WndProc(Message& m)
    at System.Windows.Forms.ButtonBase.WndProc(Message& m)
    at System.Windows.Forms.Button.WndProc(Message& m)
    at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
    at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
    at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
    at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
    at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
    at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
    at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
    at System.Windows.Forms.Application.Run(Form mainForm)
    at WindowsFormsApplication1.Program.Main() in o:\Housekeeper\test3\WindowsFormsApplication1\Program.cs:line 19
    at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
    at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
    at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
    at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
    at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
    at System.Threading.ThreadHelper.ThreadStart()
    InnerException:

    ReplyDelete
  4. This was exactly what i was looking for.
    Thank you for providing this.

    ReplyDelete
  5. @Anonymous

    For the BadImage problem, make sure the platform is the same with MediaInfo when it is compiled. Both should be 32-bit app or both are 64-bit app.

    http://stackoverflow.com/questions/15184713/c-sharp-publish-application-with-mediainfo-dll

    ReplyDelete
  6. ... for metadata of mp3 (name,author,title,album,artist, genre, ...) is possible? thanks

    ReplyDelete
    Replies
    1. Yesssssssssssssssssss. 1.000.000 thanks!!!! good work!!! :)

      Delete
    2. avifile=example.mp3

      Console.WriteLine("Album : {0}", aviFile.General.Properties.Item("Album"));
      Console.WriteLine("Genre : {0}", aviFile.General.Properties.Item("genre"));

      ...

      Delete
    3. Hey anonymous how can u call the Item in Properties because I couldn't find any function Item in Properties.

      Delete
  7. Can't thank you enough! Brilliant.

    ReplyDelete
  8. ... ex. vb.net mp3file ...

    picturebox.image = mp3file.image..... ?????

    Thanks

    ReplyDelete
  9. I've been looking for a couple of days for something like that. Thank you very much, and greetings from Spain.

    ReplyDelete
  10. How to detect the field order in interlaced ?
    Thanks

    ReplyDelete
    Replies
    1. Console.WriteLine("Scan order: {0}", aviFile.Video(0).Properties.Item("Scan order"));

      Delete
    2. Have you been successful in using the below statement? I can't find any function Item in Properties. What am I wrong? Could anyone tell me what I should do?

      Delete
  11. hi,

    how to get the rotation information from the video file

    ReplyDelete
  12. The field SamplingRate of an Audio is bugged. It doesn't consider the System.Globalization.CultureInfo.InvariantCulture when parse the value to double. In some regions, the conversion of 48.0 in string is 480 in double.

    ReplyDelete
  13. The Download link is broken

    ReplyDelete
    Replies
    1. The link is working. If you are unable to download please try after sometime or select another mirror from the SourceForge download page.

      Delete
  14. Would you mind telling me how to print out scan order of a media file?

    ReplyDelete
    Replies
    1. Console.WriteLine("Scan order: {0}", aviFile.Video(0).Properties.Item("Scan order"));

      Delete
    2. This comment has been removed by the author.

      Delete
    3. Thank you. But it doesn't have Item on Properties. Can you tell me in the details?

      Delete
    4. Try using Properties("scan order") or GetProperty("Scan order"). Both will work.

      Delete
    5. It works. Thank you so much

      Delete
  15. I have tried following your instructions but I am receiving an error when I try to add the MediaInfo.dll as a reference. I get: A reference to [Path] could not be added. Please make sure that the file is accessible, and that it is a valid assembly or COM component

    ReplyDelete
    Replies
    1. I am having this same problem

      Delete
    2. Don't add the reference to MediaInfo.dll directly. Add a reference to MediaInfoNET.dll. MediaInfoNET.dll is the .NET wrapper which calls MediaInfo.dll.

      Delete
  16. I have downloaded the files and gotten this to work as explained on local files. However when trying this with an uploaded file on https it does not seem to return the media information even when passing the full server map path on which the video is stored

    ReplyDelete
    Replies
    1. It works with only local files. You can modify the source code for handling files on a server.

      Delete
  17. Is there any way to find the rotation of a video?

    ReplyDelete
  18. ---------thanks for the information so much , i am getting this exception :An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)
    when i try to use mediaInfo the way you stated on your post ,
    I tried changing the platform in build to x86,x64,Any CPU , but it doesn't help , do you have a solution for such issue , i really would like this to work for me :(

    ReplyDelete
  19. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
    2. I have same problem. Built source as x64 project on Win 7/64 to get a dll. Made a new project and added reference to the dll. Tried several browsers, IE64, Chrome etc.
      Anybody have a solution?

      Delete
    3. Use the 64-bit version of MediaInfo.dll. The 32-bit DLL will not work on 64-bit Windows.

      http://mediaarea.net/en/MediaInfo/Download

      Delete
  20. How do you access the thumbnail image? [Cover_Data]

    ReplyDelete
    Replies
    1. The thumbnail image is not a property. You have to generate it using some other tool.

      Delete
  21. Thank you for putting me on to this! Exactly what I needed :)

    ReplyDelete
  22. Man you are a life saver! Big timesaver! Thanks!

    ReplyDelete
  23. Hi Tony

    I'm trying to read height and width information for a folder full of images as follows:-

    Dim im As New MediaFile("d:\img0001.jpg")
    Dim w, h As Integer
    Dim n As String
    n = im.Name
    MsgBox(n) 'this works ok
    w = im.Image(0).Width 'this is where it throws the exception
    h = im.Image(0).Height
    MsgBox(w, h)

    The routine works if there is only one image in the folder. However, if I use it on a folder with more than one image, I get an ArgumentOutOfRange Exception immediately after passing the first MsgBox.

    I suspect I'm doing something stupid, but any advice you can give would be much appreciated

    Geoff

    ReplyDelete
  24. Hi,

    Thanks so much for this. Is there a way to iterate round all property elements and extracting the property name and value without specifying each one specifically?

    i.e., listing all available Audio properties
    listing all available Video proeprties

    Any help gratefully appreciated

    Steven



    ReplyDelete

If you are reporting an issue and commenting as an anonymous user, please leave your email address so that I can get in touch with you.