MPEG Video Header Info
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14

Thread: MPEG Video Header Info

  1. #1
    Join Date
    Dec 2001
    Posts
    6,332

    MPEG Video Header Info

    Hi All,

    I am trying to get the info from the headers of an mpeg video file. I got what seem to be accurate results from the video header, but I am uncertain how to get the audio header. I have some ideas, but before I go off in all the wrong directions, I figured I should post the question and see who has dealt with this before. I have searched everywhere, but have not found an answer. Here is all the info I have found on the headers:

    ***************************************
    * ISO/IEC 11172(MPEG-1)/13818(MPEG-2) *
    * MPEG Bit Stream Quick Reference *
    ***************************************

    - values use big endian (network) byte/bit order
    - general terms: integer = signed value
    - general values: byte/char/octet = 8-bit value; short/word = 16-bit value; long = 32-bit value
    - fixed point values: value made up of an integer for whole numbers and an unsigned value for the decimal
    - binary values: base-2 long unsigned values (values from 0 and 1)
    - octal values: base-8 long unsigned values (values from 0 through to 7)
    - decimal values: base-10 long unsigned values (values from 0 through to 9)
    - hexadecimal (hex) values: base-16 long unsigned values (values from 0 to 9 and A to F)

    ISO/IEC 11172-2 supports a maximum bit rate of 1.5 Mbits/sec (187.5 kB/sec).
    ISO/IEC 13818-2 levels support bit rates from 3 Mbits/sec (375 kB/sec)
    to 15 Mbits/sec (1.875 MB/sec).
    ISO/IEC 14496-2 (MPEG-4 video) supports scalable bit rates from 10 kbits/sec (1.25 kB/sec)
    to 6 Mbits/sec (750 kB/sec).

    ISO/IEC 11172-2/13818-2 ELEMENTARY VIDEO HEADER

    -> 4 bytes video sequence start code = long hex value of 0x000001B3
    -> 12 bits horizontal pixels = 1 1/2 bytes unsigned video size
    -> 12 bits vertical pixels = 1 1/2 bytes unsigned video size
    -> 4 bits pixel aspect ratio = 1/2 byte unsigned aspect type
    - v1 pixel types are Reserved = 0; 1:1 = 1; 0.6735 = 2; 0.7031 = 3
    - v1 pixel types are 0.7615 = 4; 0.8055 = 5; 0.8437 = 6; 0.8935 = 7
    - v1 pixel types are 54:59 PAL = 8; 0.9815 = 9; 1.0255 = 10; 1.0695 = 11
    - v1 pixel types are 11:10 NTSC = 12; 1.1575 = 13; 1.2015 = 14; Reserved = 15
    - v2 display types are Reserved = 0; 1:1 = 1; 4:3 = 2; 16:9 = 3; 2.2:1 = 4
    - v2 display types are Reserved or v1 pixel types = 5 - 15
    -> 4 bits frame rate = 1/2 byte unsigned fps type
    - types are Reserved = 0; 23.976 frames/sec = 1; 24 frames/sec = 2
    - types are 25 frames/sec = 3; 29.97 frames/sec = 4; 30 frames/sec = 5
    - types are 50 frames/sec = 6; 59.94 frames/sec = 7; 60 frames/sec = 8
    - types are Reserved = 9 - 15
    -> 18 bits picture rate = 2 1/4 bytes unsigned data bit rate in 400 bps units
    -> 1 bit header marker flag = 1/8 byte true/false value
    -> 10 bits VBV buffer size = 1 1/4 bytes unsigned decompression buffer in 16,000 byte units
    -> 1 bit constrained parameter flag = 1/8 byte true/false value
    -> 1 bit load intra quantizer matrix flag = 1/8 byte true/false value
    -> 64 byte quantizer table - only if above is true otherwise no values
    -> 1 bit load intra non quantizer matrix flag = 1/8 byte true/false value
    -> 64 byte quantizer table - only if above is true otherwise no values
    -> Continues with other MPEG video start codes and associated data like GOPs, pictures and slices
    - NOTE: only one video header per file bit stream

    -> 4 bytes video sequence end start code = long hex value of 0x000001B7
    - NOTE: only one video sequence end to terminate decoding of video bit stream


    ISO/IEC 11172-3/13818-3 ELEMENTARY AUDIO FRAME PACKET HEADER

    -> 11 bits audio frame/packet sync marker = 1 3/8 bytes unsigned value of 2047
    -> 2 bits audio version = 1/4 byte unsigned version type
    - types are Fraunhofer (v2.5) = 0; Reserved = 1; ISO/IEC 13818-3 (v2) = 2; ISO/IEC 11172-3 (v1) = 3
    -> 2 bits audio compression layer/level = 1/4 byte unsigned level type
    - types are Reserved = 0; Fraunhofer layer III = 1; ISO/IEC layer II = 2; ISO/IEC layer I = 3
    -> 1 bit CRC data protection flag = 1/8 byte true/false type
    - short unsigned CR check value added after header = 0; not used = 1
    -> 4 bits audio packet rate/size = 1/2 bytes unsigned data bit rate type
    - NOTE: L1 = layer I; L2 = layer II; L3 = layer III
    - types are Reserved = 0; v1 - 32 kbps/v2 L2/3 - 8 kbps = 1
    - types are v1 L1- 64kbps or v1 L2/v2 L1- 48kbps or v1 L3- 40kbps or v2 L2/3-16kbps or v2.5 L3-12kbps = 2
    - types are v1 L1- 96kbps or v1 L2/v2 L1- 56kbps or v1 L3- 48kbps or v2 L2/3-24kbps or v2.5 L3-20kbps = 3
    - types are v1 L1-128kbps or v1 L2/v2 L1- 64kbps or v1 L3- 56kbps or v2 L2/3-32kbps = 4
    - types are v1 L1-160kbps or v1 L2/v2 L1- 80kbps or v1 L3- 64kbps or v2 L2/3-40kbps = 5
    - types are v1 L1-192kbps or v1 L2/v2 L1- 96kbps or v1 L3- 80kbps or v2 L2/3-48kbps = 6
    - types are v1 L1-224kbps or v1 L2/v2 L1-112kbps or v1 L3- 96kbps or v2 L2/3-56kbps = 7
    - types are v1 L1-256kbps or v1 L2/v2 L1-128kbps or v1 L3-112kbps or v2 L2/3-64kbps = 8
    - types are v1 L1-288kbps or v1 L2-160kbps or v1 L3-128kbps or v2 L1-144kbps or v2 L2/3-80kbps = 9
    - types are v1 L1-320kbps or v1 L2-192kbps or v1 L3/v2 L1-160kbps or v2 L2/3-96kbps = 10
    - types are v1 L1-352kbps or v1 L2-224kbps or v1 L3-192kbps or v2 L1-176kbps or v2 L2/3-112 kbps = 11
    - types are v1 L1-384kbps or v1 L2-256kbps or v1 L3-224kbps or v2 L1-192kbps or v2 L2/3-128 kbps = 12
    - types are v1 L1-416kbps or v1 L2-320kbps or v1 L3-256kbps or v2 L1-224kbps or v2 L2/v2.5 L3-144kbps = 13
    - types are v1 L1-448kbps or v1 L2-384kbps or v1 L3-320kbps or v2 L1-256kbps/v2 L2/3-160kbps = 14
    - types are Reserved = 15
    - NOTE for L3: v1 sample rates require 64 kbps per channel; v2 sample rates require 32 kbps per channel
    -> 2 bits audio sample rate = 1/4 byte unsigned rate type
    - types are v1 - 44100 Hz/v2 - 22050 Hz/v2.5 - 11025 Hz = 0
    - types are v1 - 48000 Hz/v2 - 24000 Hz/v2.5 - 12000 Hz = 1
    - types are v1 - 32000 Hz/v2 - 16000 Hz/v2.5 - 8000 Hz = 2
    - types are Reserved = 3
    -> 1 bit frame/packet data padding space flag = 1/8 byte true/false type
    - not used = 0; byte (L2/3) - long (L1) byte slot added at packet end = 1
    -> 1 bit private flag = 1/8 byte true/false value
    -> 2 bits audio channels = 1/4 byte unsigned channel type
    - types are Stereo = 0; Joint Stereo = 1; Dual Monaural = 2; Monaural = 3
    -> 2 bits audio joint stereo mode = 1/4 byte unsigned mode type
    - types are none = 0; intensity = 1; ms = 2; intensity/ms = 3
    -> 1 bit audio copyright flag = 1/8 byte true/false value
    -> 1 bit audio original flag = 1/8 byte true/false value
    -> 2 bits audio emphasis = 1/4 byte unsigned emphasis type
    - types are none = 0; 50/15 ms = 1; Reserved = 2; CCIT J.17 = 3
    -> Continues with associated compressed data/slots and possibly more MPEG audio frame packets


    Anyone have any ideas?

  2. #2
    Join Date
    Feb 2003
    Location
    AR
    Posts
    228

    Re: MPEG Video Header Info

    Sorry to revive an old topic, but you probably can help me out.

    I only need to get the FPS from the MPEG file, but I have no idea how to process it to get the info even with the above specifications. Any example will be really appreciated.

    I found an excellent code here to retrieve all info from AVI files, but I also need to get FPS from MPG vids as well.

  3. #3
    Join Date
    Dec 2001
    Posts
    6,332

    Re: MPEG Video Header Info

    Actually, it's true I made progress - more so than I originally anticipated

    As you may have found by now, the MPEG format carries some copyrights and such, so I can't legally post the really good stuff here

    I'll send you a PM later...
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  4. #4
    Join Date
    Feb 2003
    Location
    AR
    Posts
    228

    Re: MPEG Video Header Info

    Don't worry, I only want to query the FPS factor, I am not developing an MPEG P2P sharing application nor anything like that. Copyrights will remain intact.

  5. #5
    Join Date
    Dec 2001
    Posts
    6,332

    Re: MPEG Video Header Info

    Quote Originally Posted by Andrex
    Don't worry, I only want to query the FPS factor, I am not developing an MPEG P2P sharing application nor anything like that. Copyrights will remain intact.
    Yeah, and all I wanted was to determine similar characteristics, like the video size, length, etc. It's a shame when things such as this are kept from developers unless they pay exorbitant fees.
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  6. #6
    Join Date
    Nov 2006
    Posts
    1

    Re: MPEG Video Header Info

    it seems that the topic is pretty old but I would like to know if I may receive all what you got about mpeg format. I have to code a video convertor from mpeg-2 to mpeg-4 for my study and it's pretty hard to find the good informations I need.

    thanks

  7. #7
    Join Date
    Dec 2001
    Posts
    6,332

    Re: MPEG Video Header Info

    If I'm not mistaken, what you'll need to do is get the data from one codec, and pass it directly to another for recoding. There are programs such as VirtualDub, which is freely available including the source code. Though not done in VB, a Google search should turn up something.

    But obviously the course you're taking is intended to develop your coding skills, so you would be cheating yourself to "borrow" much from someone else's work.
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  8. #8
    Join Date
    Mar 2006
    Posts
    3

    Re: MPEG Video Header Info

    Quote Originally Posted by WizBang
    Actually, it's true I made progress - more so than I originally anticipated

    As you may have found by now, the MPEG format carries some copyrights and such, so I can't legally post the really good stuff here

    I'll send you a PM later...
    please give me some information regarding mpeg format

  9. #9
    Join Date
    Dec 2001
    Posts
    6,332

    Re: MPEG Video Header Info

    If you want to decode the video/audio data, that I cannot help you with, as I haven't attempted it. If you want to gather the header information, my first post in this thread gives quite a few details.
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  10. #10
    Join Date
    Mar 2006
    Posts
    3

    Re: MPEG Video Header Info

    Quote Originally Posted by WizBang
    If you want to decode the video/audio data, that I cannot help you with, as I haven't attempted it. If you want to gather the header information, my first post in this thread gives quite a few details.


    thanks fo rthe quick reply
    i want to recover corrupted mpg file
    for that i want two key information
    first to identify the file as mpg
    second to calculate size
    your first post tells about mpg having "0x 00 00 01 b3"header
    but i am having files which have got header "0x 00 00 01 ba"
    some of these files do not have end code as "0x00 00 01 b7"
    how to calculate size for these files
    thanks and regards
    suchira

  11. #11
    Join Date
    Dec 2001
    Posts
    6,332

    Re: MPEG Video Header Info

    Unless you want to develop a video recovery application, it might be better to simply use a program such as VirtualDub.
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  12. #12
    Join Date
    Mar 2006
    Posts
    3

    Unhappy Re: MPEG Video Header Info

    Quote Originally Posted by WizBang
    Unless you want to develop a video recovery application, it might be better to simply use a program such as VirtualDub.
    yes that is the main problem i have to design an application to recover mpeg video file

  13. #13
    Join Date
    Dec 2001
    Posts
    6,332

    Re: MPEG Video Header Info

    I see. Well, I suppose what I'd probably start with is to write a routine that can scan the file for video frame headers, determine the length, and copy the data to a new file. In that way, it could skip any invalid frame data. You could also set the dimensions for the frames if they happen to be incorrect, though determining the true dimensions would seem somewhat more involved. The audio data could be parsed in much the same way.

    Just how well this idea would work is another matter though, and I doubt it would always work as intended.
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  14. #14
    Join Date
    Apr 2014
    Posts
    1

    Re: MPEG Video Header Info

    Here is a cheat to use MediaInfo from source forge http://mediaarea.net/en/MediaInfo to pull out meta data from video files using VB6...

    This is not the perfect design. DLLS are not loaded - it works by directing console output to a buffer and pulls it back in for parsing. No interim output file writing and reading is necessary (other than the current bug with MediaInfo CLI where parms are not being accepted on the command line and must be entered in a TXT file).

    This bit of code consists of many parts of other examples that can be found online. If anyone finds a loophole, let me know and I'll update this. Please note this example works with the CLI version of MediaInfo.exe. It requires that both MediaInfo.exe and MediaInfo.dll from the >>>CLI<<< version be present in the root directory of the Visual Basic 6 application. Don't get mixed up and download and try instead to run the GUI version!

    This example uses a system32 call to pipe the console from MediaInfo to a buffer and it returns the contents of that buffer from the function call ExecCmdCons.

    THIS IS NOT VB.NET - THIS IS VB6!

    1) Create a module mShellCmdCons.bas:

    Code:
    Attribute VB_Name = "mShellCmdCons"
    Option Explicit
    Private Type STARTUPINFO
       cb As Long
       lpReserved As String
       lpDesktop As String
       lpTitle As String
       dwX As Long
       dwY As Long
       dwXSize As Long
       dwYSize As Long
       dwXCountChars As Long
       dwYCountChars As Long
       dwFillAttribute As Long
       dwFlags As Long
       wShowWindow As Integer
       cbReserved2 As Integer
       lpReserved2 As Long
       hStdInput As Long
       hStdOutput As Long
       hStdError As Long
    End Type
    
    Private Type PROCESS_INFORMATION
       hProcess As Long
       hThread As Long
       dwProcessID As Long
       dwThreadID As Long
    End Type
    
    Private Type SECURITY_ATTRIBUTES
        nLength                            As Long
        lpSecurityDescriptor               As Long
        bInheritHandle                     As Long
    End Type
     
    Private Const STARTF_USESHOWWINDOW     As Long = &H1
    Private Const STARTF_USESTDHANDLES     As Long = &H100
    Private Const SW_HIDE                  As Integer = 0
    Private Const NORMAL_PRIORITY_CLASS = &H20&
    Private Const INFINITE = -1&
    Private Const BUFSIZE As Long = 1024 * 10
    
    Private Declare Function CreatePipe Lib "kernel32" (phReadPipe As Long, phWritePipe As Long, lpPipeAttributes As Any, ByVal nSize As Long) As Long
    Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, lpOverlapped As Any) As Long
    Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
    Private Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" (ByVal lpApplicationName As String, ByVal lpCommandLine As String, lpProcessAttributes As Any, lpThreadAttributes As Any, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, lpEnvironment As Any, ByVal lpCurrentDriectory As String, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long
    Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
    
    
    Public Function ExecCmdCons(ByVal cmdLine As String, Optional ByVal sStartInFolder As String = vbNullString) As String
        Dim pi As PROCESS_INFORMATION
        Dim si As STARTUPINFO
        Dim sa As SECURITY_ATTRIBUTES
        Dim ret As Long
        Dim consOut As String
        
        Dim hPipeRead         As Long
        Dim hPipeWrite        As Long
        Dim baOutput(BUFSIZE) As Byte
        Dim sOutput           As String
        Dim lBytesRead        As Long
        
        Dim pTimeStamp As Long
        Dim pDateTime As String
        
        sa.nLength = Len(sa)
        sa.bInheritHandle = 1
        
        If CreatePipe(hPipeRead, hPipeWrite, sa, 0) = 0 Then
         Exit Function
        End If
        
        si.cb = Len(si)
        si.dwFlags = STARTF_USESHOWWINDOW Or STARTF_USESTDHANDLES
        si.wShowWindow = SW_HIDE
        si.hStdOutput = hPipeWrite
        si.hStdError = hPipeWrite
        
        ret = CreateProcess(vbNullString, cmdLine, ByVal 0&, ByVal 0&, 1, 0&, ByVal 0&, sStartInFolder, si, pi)
        consOut = ""
        If ret = 1 Then
            ret = WaitForSingleObject(pi.hProcess, INFINITE)
            Call GetExitCodeProcess(pi.hProcess, ret)
            Call CloseHandle(hPipeWrite)
            Call CloseHandle(pi.hThread)
            hPipeWrite = 0
            Do
               DoEvents
               If ReadFile(hPipeRead, baOutput(0), BUFSIZE, lBytesRead, ByVal 0&) = 0 Then
                   Exit Do
               End If
               sOutput = Left$(StrConv(baOutput(), vbUnicode), lBytesRead)
               consOut = consOut & sOutput
            Loop
            Call CloseHandle(pi.hProcess)
            ExecCmdCons = consOut
        End If
        Call CloseHandle(hPipeRead)
        Call CloseHandle(hPipeWrite)
    End Function
    2) Create a Input file: (can be any parms you want, or leave off the call to
    template.txt:

    General;File Name...........: %FileName%\nEncoced date........: %Encoded_Date%\nFile Size...........: %FileSize/String3%\nFormat..............: %Format%\n
    Video;Video Bitrate.......: %BitRate/String%\nVideo Codec.........: %Format% %Format_Profile%\nAspect Ratio........: %DisplayAspectRatio/String%\nResolution..........: %Width%x%Height%\nFrame Rate..........: %FrameRate/String%\n
    Audio;Audio Codec.........: %Codec/String%$if(%BitRate%, @ %BitRate/String%)\nChannels............: %Channel(s)% CH\n$if(%Language%,Language............: %Language/String%)\n
    Text;Subtitle : %Language/String%\n

    3) Create a form "form1.frm":
    Code:
    VERSION 5.00
    Begin VB.Form Form1 
       Caption         =   "Form1"
       ClientHeight    =   3030
       ClientLeft      =   120
       ClientTop       =   450
       ClientWidth     =   4560
       LinkTopic       =   "Form1"
       ScaleHeight     =   3030
       ScaleWidth      =   4560
       StartUpPosition =   3  'Windows Default
       Begin VB.CommandButton cmd_DoIt 
          Caption         =   "Do It"
          Height          =   615
          Left            =   720
          TabIndex        =   0
          Top             =   840
          Width           =   2175
       End
    End
    Attribute VB_Name = "Form1"
    Attribute VB_GlobalNameSpace = False
    Attribute VB_Creatable = False
    Attribute VB_PredeclaredId = True
    Attribute VB_Exposed = False
    Public vbQuote As String
    
    Private Sub form_load()
         vbQuote = Chr(34) 'do not change or delete this
    End Sub
    
    Private Sub cmd_DoIt_Click()
        Dim pTimeStamp As Long
        Dim cmdRet As String
            
        'cmdRet = ExecCmdCons("MediaInfo --Inform=file://template.txt --LogFile=info.txt TEST.MOV") 'movie is in app directory
        'cmdRet = ExecCmdCons("MediaInfo --Inform=file://template.txt --LogFile=info.txt " & vbQuote & "C:\Program Files (x86)\MVI_8964.MOV" & vbQuote) 'spec return parms, log to file and return console
        'cmdRet = ExecCmdCons("MediaInfo --LogFile=info.txt " & vbQuote & "C:\Program Files (x86)\MVI_8964.MOV" & vbQuote) 'default parms returned, log to file and return console; example dir with spaces
         cmdRet = ExecCmdCons("MediaInfo --Inform=file://template.txt " & vbQuote & "C:\Program Files (x86)\MVI_8964.MOV" & vbQuote) 'spec return parms, no log file and dir with spaces
        If cmdRet <> "" Then
            pTimeStamp = InStr(cmdRet, ": UTC ")
            If pTimeStamp > 0 Then
                pDateTime = Mid(cmdRet, pTimeStamp + 6, 19)
                MsgBox "Date Movie Taken: " & pDateTime
            End If
        End If
    End Sub

    Now the above is a simple VB6 form with a button that calls the Sub cmd_DoIt(). You can put your own parms in to call the command ExecCmdCons with MediaInfo or any other command which only outputs to the console. In this example the MediaInfo.exe command string is run and in turn the result is returned in the variable cmdRet.

    Notes: See how file locations with spaces in the name are handled with the vbQuote variable to insert quotes at the beginning and end (I use this because multiple quotes to accomplish the same really confuse me).
    Notes: See that the input file example template.txt is not needed if you leave off the --Inform=filexxxxxxx which assumes you want the default batch of info back. Not sure if adding the file read saves anything by trimming the output since output file is not needed
    Notes: A log file is not necessary, as the console is captured directly, but it is available if you need it

    Finally, if you build something cool with this, please share it with me ;-)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)