tags:

views:

2878

answers:

8

I am trying to read metadata from a file. I only need the Video -> Length property, however I am unable to find a simple way of reading this information.

I figured this would be fairly easy since it is visible by default in Explorer, however this looks to be way more complicated than I anticipated. The closest I came was using:

Microsoft.DirectX.AudioVideoPlayback.Video video = new Microsoft.DirectX.AudioVideoPlayback.Video(str);
double duration = video.Duration;

However this throws a LoaderLock exception, and I don't know how to deal with it.

Any ideas?

+2  A: 

Take a look at http://www.taoframework.com/project/ffmpeg. It's a .Net wrapper for using ffmpeg to work with most video formats/codecs. That way you don't need to worry about the codec being installed on the machine.

Or look at http://mediainfo.sourceforge.net/en.

Mikael Svenson
+2  A: 

I would have just commented on Mikael's post, but I don't quite have enough rep to do it yet. I agree with him on using ffmpeg so that you don't have to require that codecs be installed. You could just parse the output of "ffmpeg -i your_filename" which will just dump some info about the video including the duration.

I don't know what codecs you're working with, but some containers do not actually store the duration in the metadata (this is common of streaming containers since duration is unknown). I don't know how ffmpeg handles this, but it seems to find it somehow (maybe by parsing the whole file for timecodes).

Jason
A: 

Hi, try this link hope it work for you

http://bellouti.wordpress.com/2007/09/28/determine-a-video-size/

Best Regards, Iordan

IordanTanev
A: 

MediaInfo is a great open source library for that purpose (the DLL is licensed LGPL). The download package contains sample application in C# (under Developers\Project\MSCS\Example)

hmemcpy
+1  A: 

Many of these details are provided by the shell, so you can do this by adding a reference to the COM Library "Microsoft Shell Controls and Automation" (Shell32), and then using the Folder.GetDetailsOf method to query the extended details.

I was recently looking for this and came across this very question on the MSDN C# General forums. I wound up writing this as an extension method to FileInfo:

    public static Dictionary<string, string> GetDetails(this FileInfo fi)
    {
        Dictionary<string, string> ret = new Dictionary<string, string>();
        Shell shl = new ShellClass();
        Folder folder = shl.NameSpace(fi.DirectoryName);
        FolderItem item = folder.ParseName(fi.Name);

        for (int i = 0; i < 150; i++)
        {
            string dtlDesc = folder.GetDetailsOf(null, i);
            string dtlVal = folder.GetDetailsOf(item, i);

            if (dtlVal == null || dtlVal == "")
                continue;

            ret.Add(dtlDesc, dtlVal);
        }
        return ret;
    }

If you're looking for specific entries, you can do something similar, though it will be far faster to find out what index those entries are at (Length is index 27 I believe) and just query those. Note, I didn't do much research into whether or not the index can change (I doubt it), which is why I took the dictionary approach.

chsh
You cant call shell like that any more, http://social.msdn.microsoft.com/Forums/en-US/vsxprerelease/thread/f7ac34d9-fea7-44e3-a375-aadcbaf85c65, var shl = (Shell) Activator.CreateInstance(Type.GetTypeFromProgID("Shell.Application"));or var shl2 = (Shell)Marshal.GetActiveObject("Shell.Application");
RandomNickName42
A: 

I had the same issue with a small video preview app.

The issue is Managed Debugging Assisstants. This is an issue when using The Managed DirectX 1.1 libraries in VS2005 or 2008. Microsoft has moved on to focus on MDX2 and then XNA rather than Managed DirectX 1 so don't hope too much for a patch.

The easy workaround is to disable the LoaderLock Exception handling while debugging that solution. This should have no real effect on the program anyways since this error only shows up in a debug environment.

To disable go to Debug -> Exceptions -> Managed Debugging Assistants and uncheck LoaderLock.

More info here:http://vivekthangaswamy.blogspot.com/2006/11/loaderlock-was-detected-error-when.html

Brian Duncan
+1  A: 

using DirectShowLib (http://directshownet.sourceforge.net/)

   /// <summary>
    /// Gets the length of the video.
    /// </summary>
    /// <param name="fileName">Name of the file.</param>
    /// <param name="length">The length.</param>
    /// <returns></returns>
    static public bool GetVideoLength(string fileName, out double length)
    {
        DirectShowLib.FilterGraph graphFilter = new DirectShowLib.FilterGraph();
        DirectShowLib.IGraphBuilder graphBuilder;
        DirectShowLib.IMediaPosition mediaPos;
        length = 0.0;

        try
        {
            graphBuilder = (DirectShowLib.IGraphBuilder)graphFilter;
            graphBuilder.RenderFile(fileName, null);
            mediaPos = (DirectShowLib.IMediaPosition)graphBuilder;
            mediaPos.get_Duration(out length);
            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            mediaPos = null;
            graphBuilder = null;
            graphFilter = null;
        }
    }
mneves