Question

I'm trying to sniff the type of a file, based on the magic numbers. After some googling I've found the following information:

WMA:

Primary association: Windows Media
Company: Microsoft Corporation
File classification: Audio
Mime type: audio/x-ms-wma, video/x-ms-asf
Identifying characters Hex: 30 26 B2 75 8E 66 CF 11 A6 D9 00 AA 00 62 CE 6C

WMV:

Primary association: Windows Media File
Company: Microsoft Corporation
Mime type: video/x-ms-wmv
Identifying characters Hex: 30 26 B2 75 8E 66 CF 11 A6 D9 00 AA 00 62 CE 6C

These magic numbers are used by both audio and video. How can I detect if I have the one or the other. Note: I can't use an extension, I only have a System.IO.Stream with bytes.

Était-ce utile?

La solution

This depends on how good your sniffer should be and how good performance should be. If you can analyse some 1024 bytes, then you can search for some other metadata specific for WMV files. If there is no such data you can assume it's a WMA.

Look for following bytes in (in preffered order):

  1. AspectRatio (UTF-16)
  2. WindowsMediaVideo (UTF-16)
  3. WMV3 (ASCII)
  4. DeviceConformanceTemplate MP@ML (UTF-16)

Presense any of this metadata tags in header region identifies file as WMV. So in worst case you will need to perform about 8 searches (including all DeviceConformanceTemplate's types) in 1024 bytes region to detect WMV, and in best case 1 search to detect WMV.

Other way is to parse header (AsfMojo might help) which can be some typical 8K bytes, but not limited in general. I assume header information must be enought to distingush between audion and video data (but I'm not 100% sure). I would prefer this way, and fallback to "hacky" solution if parsing not possible.

Autres conseils

From MSDN:

The only difference between ASF files and WMV or WMA files are the file extensions and the MIME types. The MIME type for a WMV file is video/x-ms-wmv, and for WMA it is audio/x-ms-wma. The MIME type for ASF is video/x-ms-asf. The basic internal structure of the files is identical.

As already mentioned, WMV and WMA filea share the same format - Advanced Systems Format (ASF). Well, you can check ASF format specification on MS website (it is available), but it is not for the faint of heart.

To tell what's inside the files, you perhaps want to instantiate Windows Media Reader Object with WMCreateReader function and use IWMHeaderInfo interface to retrieve information about the file.

WindowsMedia.NET Library will give you definition required to work with this API from C# (it perhaps also has useful samples).

You will not be able to tell the difference just looking at the magic number - the magic number is of the container format which is the Advanced Systems Format (ASF).

ASF may contain any number of streams including audio and video streams. Only if at least one video stream is present you would consider the file a WMV file.

If you do want to differentiate between WMA and WMV (and not just ASF) you will have to parse the media file, e.g. using AsfMojo.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top