Search code examples
javavideoaviwmv

Format of a .wmv file. (or any video file would be great)


I want to take a video file and "encrypt" it using java. For now encrypting is just flipping the bits. I want the video to still be able to play, so the header needs to be intact. I'm finding it very difficult to find how big the header is. I tried with a .avi file and assumed the header size with this link AVI file details but this didn't seem to work. I then eventually guessed at leaving the first 40kb (seems very large?) intact and then flipped all the bits which followed. This succeeded I guess although the video gets buggy at the end, but it's not really acceptable to guess at 40kb. I then read here that .avi files have a trailer so I have decided to avoid them for the moment to avoid this extra complication.

Could anybody tell me what the format of a .wmv file is, crucially the size of it's header. If not .wmv any popular video file would do!

Apologies if this is unclear.


Solution

  • As others have noted the ASF specification fully covers the format of WMV/WMA media files. If C# is an option for you there is AsfMojo which is an ASF file parser, i.e. the header size you can easily determine using properties of the AsfFile class.

    using (AsfFile asfFile = new AsfFile(@"C:\samples\sample.wmv"))
    {
        uint headerSize = asfFile.PacketConfiguration.AsfHeaderSize;
    }
    

    This will not allow you to scramble the data though, as this would make the file invalid. You don't want to scramble the packets, you want to scramble individual payloads and leave everything else intact. This is usually done (as far as I am aware) by using a codec that you need licensing for, otherwise the file won't play back.

    For just getting access to the payloads you could do something like this:

    using (AsfFile asfFile = new AsfFile(@"C:\samples\sample.wmv"))
    {
        var dataObject = asfFile.GetAsfObject<AsfDataObject>();
        foreach (var packet in dataObject.Packets)
            foreach(var payload in packet.Payload)
            {
                //do something with the payload
            }
        }
    }
    

    If you cannot use C# you can use the project at least as a guide on how to parse ASF files in general.