Search code examples
c#unity-game-engine

How to read MP4 video metdata and get the video's Major Brand


I am trying to read the metadata that exists in the MP4 video file in order to get the Major Brand ( A.K.A ftyp ) value that exists in it, but I could not find any existing methods to do so in Unity. So, I would like to know are there any methods/solutions in order to achieve this?


Solution

  • After a lot of time wasted, I finally have a solution to retrieve the value of MajorBrand from Mp4 video. I am sharing my solution here, hope it will be able to help other people

    FileStream fileStream = new FileStream( filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite );
    
    BinaryReader binaryReader = new BinaryReader( fileStream );
    binaryReader.BaseStream.Seek( 0, SeekOrigin.Begin );
    byte[] verifyArray = binaryReader.ReadBytes( 32 );
    binaryReader.Close();
    
    ConvertToString( BitConverter.ToString( verifyArray ) );
    
    private void ConvertToString( string hex )
    {
        StringBuilder builder = new StringBuilder();
    
        string[] splitHex = hex.Split( new char[] { '-' } );
        for (int i = 0; i < splitHex.Length; i++)
        {
           int value = Convert.ToInt32( splitHex[i], 16 );
           string stringValue = char.ConvertFromUtf32( value );
           char charValue = (char)value;
            if( value != 0 )
               builder.Append( stringValue );
         }
     }