Meet us at PRINT 19. Chicago, IL. Oct. 3 - 5.
This documentation is for the old version. Go to the latest Graphics Mill docs

Determining Audio/Video File Length

If you need to determine the length of some audio or video file, you can do it in two ways:

As metadata are not always present in the file, the second approach is used in this example.

The function that will determine the duration of the file should perform the following operations:

  1. Create a reader and open the source file.
  2. Read the appropriate Duration property depending on the reader used.
  3. Close the reader.
Visual Basic
'Returns the value measured in 1/100s of a second
Public Function GetLength(ByVal inputFilename As String) As Int32
    Dim duration As Int32
    Try
        'Create the reader
        Dim reader As IFormatReader = MediaFormatManager.CreateFormatReader _
         (inputFilename)
        'Get length from the reader
        If TypeOf reader Is QTReader Then
            Dim _reader As QTReader = CType(reader, QTReader)
            duration = _reader.Duration
        ElseIf TypeOf reader Is WMReader Then
            Dim _reader As WMReader = CType(reader, WMReader)
            duration = _reader.Duration
        Else
            Dim _reader As DSReader = CType(reader, DSReader)
            duration = _reader.Duration
        End If
        reader.Dispose()
        Return duration
    Catch ex As Exception
        Console.WriteLine(ex)
        Return duration
    End Try
End Function
C#
//Returns the value measured in 1/100s of a second
public static int GetLength(string inputFilename)
{
    int duration = -1;
    try
    {
        //Create the reader
        IFormatReader reader = MediaFormatManager.CreateFormatReader(inputFilename);
        //Get length from the reader
        if (reader.GetType().Equals(typeof(QTReader)))
        {
            QTReader _reader = (QTReader)reader;
            duration = _reader.Duration;
        }
        else if (reader.GetType().Equals(typeof(WMReader)))
        {
            WMReader _reader = (WMReader)reader;
            duration = _reader.Duration;
        }
        else
        {
            DSReader _reader = (DSReader)reader;
            duration = _reader.Duration;
        }
        reader.Dispose();
        return duration;
    }
    catch (System.Exception ex)
    {
        Console.WriteLine(ex);
        return duration;
    }
}

See Also

Samples

Reference

Manual