Results 1 to 4 of 4

Thread: [RESOLVED] Reliably identify video files. Was: Video file GUIDs?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2015
    Posts
    227

    Resolved [RESOLVED] Reliably identify video files. Was: Video file GUIDs?

    Hi everyone!

    I use several of these in one of my projects in order to identify image files:

    Code:
     Const ImageFormatBMP             As String = "{B96B3CAB-0728-11D3-9D7B-0000F81EF32E}"
        Const ImageFormatEMF             As String = "{B96B3CAC-0728-11D3-9D7B-0000F81EF32E}"

    But I need same for mp4, mov, avi, wmv.

    Not sure if exists or where to find? Thanks as always .

    EDIT: What I really need is a reliable way to identify video files without using FSO etc.....I'm thinking open the file in binary mode & read applicable header bytes. Thoughts appreciated!
    Last edited by SomeYguy; Apr 20th, 2024 at 06:45 PM.

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    Feb 2015
    Posts
    227

    Re: [RESOLVED] Reliably identify video files. Was: Video file GUIDs?

    Well, I decided to go simple and just read the file header. Here's what I came up with:

    Code:
    Dim d As Byte, s As Long, fI as long, st as string
    
    fI = FreeFile
    
    Open "C:\vids\ShakeItUp.mp4" For Binary As fI
    
    For s = 5 To 8
    
    Get fI, s, d
    
    st = st & CStr(Hex(d))
    
    
    Next s
    
    Close fI
    
    End Sub
    
    '- mp4 = 5 to 8, 66747970
    '- avi = 9 to 12, 41564920
    This was just a quick throw together so not production code. Seems to work well!
    Last edited by SomeYguy; Apr 20th, 2024 at 10:53 PM.

  3. #3
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,774

    Re: [RESOLVED] Reliably identify video files. Was: Video file GUIDs?

    GPT-4 gave me this:-
    Code:
    Private Sub IdentifyFileType(ByVal fileName As String)
        Dim fileNumber As Integer
        Dim header As String
        Dim buffer As String
        
        fileNumber = FreeFile ' Get a free file number
        
        On Error GoTo ErrorHandler ' Error handling if file can't be opened
        Open fileName For Binary As #fileNumber ' Open the file in binary mode
        
        ' Read the first 12 bytes from the file
        header = Space$(12)
        Get #fileNumber, , header
        Close #fileNumber ' Close the file
        
        ' Check for PNG
        If Left$(header, 8) = Chr$(137) & "PNG" & Chr$(13) & Chr$(10) & Chr$(26) & Chr$(10) Then
            MsgBox fileName & " is a PNG file."
        ' Check for JPEG
        ElseIf Left$(header, 3) = Chr$(255) & Chr$(216) & Chr$(255) Then
            MsgBox fileName & " is a JPEG file."
        ' Check for GIF
        ElseIf Left$(header, 3) = "GIF" Then
            MsgBox fileName & " is a GIF file."
        ' Check for BMP
        ElseIf Left$(header, 2) = "BM" Then
            MsgBox fileName & " is a BMP file."
        ' Check for AVI
        ElseIf Left$(header, 4) = "RIFF" And Mid$(header, 9, 4) = "AVI " Then
            MsgBox fileName & " is an AVI file."
        ' Check for MP4
        ElseIf Left$(header, 4) = Chr$(0) & Chr$(0) & Chr$(0) And _
               (Mid$(header, 5, 8) = "ftypisom" Or Mid$(header, 5, 8) = "ftypmp42") Then
            MsgBox fileName & " is an MP4 file."
        Else
            MsgBox "File type of " & fileName & " is unknown."
        End If
    
        Exit Sub
    
    ErrorHandler:
        MsgBox "Error opening file. Please check if the file exists and is accessible."
        Close #fileNumber
    End Sub
    I'd recommend double checking that those header signatures as AIs tend to hallucinate. GPT-4 rarely screws up simple things like this but I'd double check it anyway.

    I'd also recommend using ChrW instead of Chr. We live in a Unicode world now.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Feb 2015
    Posts
    227

    Re: [RESOLVED] Reliably identify video files. Was: Video file GUIDs?

    Looks better than what I came up with as far as reading the needed bytes. I'll try it along with your recommendations, thanks

Posting Permissions

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



Click Here to Expand Forum to Full Width