---
title: "Determining Audio/Video File Length"
canonical: https://www.graphicsmill.com/docs/gm5/DeterminingAudioVideoStreamLength.htm
product: "Graphics Mill 5.5 for .NET"
---

This documentation is for the old version. [Go to the latest Graphics Mill docs](https://www.graphicsmill.com/docs/gm/)

# 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:
 
- Read the [MediaProcessorMetadataDictionary.Length](https://www.graphicsmill.com/docs/gm5/T_Aurigma_GraphicsMill_Codecs_MediaProcessorMetadataDictionary.htm.md) field. This will work only if the reader supports metadata extraction and the file contains that field. For more details about reading ID3 fields, see the [Reading Metadata](https://www.graphicsmill.com/docs/gm5/ReadingMetadata.htm.md) topic.
- Read the [DSReader.Duration](https://www.graphicsmill.com/docs/gm5/P_Aurigma_GraphicsMill_Codecs_DSReader_Duration.htm.md) property (or [QTReader.Duration](https://www.graphicsmill.com/docs/gm5/P_Aurigma_GraphicsMill_Codecs_QTReader_Duration.htm.md) or [WMReader.Duration](https://www.graphicsmill.com/docs/gm5/P_Aurigma_GraphicsMill_Codecs_WMReader_Duration.htm.md)—the choice depends on the reader you use).

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.

```
'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
```
 
```csharp
//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
 
- [Reading ID3 Tags](https://www.graphicsmill.com/docs/gm5/ReadingID3.htm.md)

### Reference
 
- [DSReader.Duration Property](https://www.graphicsmill.com/docs/gm5/P_Aurigma_GraphicsMill_Codecs_DSReader_Duration.htm.md)
- [QTReader.Duration Property](https://www.graphicsmill.com/docs/gm5/P_Aurigma_GraphicsMill_Codecs_QTReader_Duration.htm.md)
- [WMReader.Metadata Property](https://www.graphicsmill.com/docs/gm5/P_Aurigma_GraphicsMill_Codecs_WMReader_Metadata.htm.md)

### Manual
 
- [Reading Metadata](https://www.graphicsmill.com/docs/gm5/ReadingMetadata.htm.md)
