Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

mediainfo.dll calling example


  • Please log in to reply
6 replies to this topic
mprost
  • Members
  • 5 posts
  • Last active: Jun 02 2014 03:25 PM
  • Joined: 15 Dec 2006
Hi.

Here's a little example of how to call mediainfo.dll.

Some details about MediaInfo (taken from http://mediainfo.sourceforge.net):

MediaInfo supplies technical and tag information about a video or audio file. This is free software (free of charge and free to access source code: GPL or LGPL licence).

What information can I get from MediaInfo?

* General: title, author, director, album, track number, date, duration...
* Video: codec, aspect, fps, bitrate...
* Audio: codec, sample rate, channels, language, bitrate...
* Text: language of subtitle
* Chapters: number of chapters, list of chapters

What format (container) does MediaInfo support?

* Video: MKV, OGM, AVI, DivX, WMV, QuickTime, Real, MPEG-1, MPEG-2, MPEG-4, DVD (VOB)...
(Codecs: DivX, XviD, MSMPEG4, ASP, H.264, AVC...)
* Audio: OGG, MP3, WAV, RA, AC3, DTS, AAC, M4A, AU, AIFF...
* Subtitles: SRT, SSA, ASS, SAMI...


Here's the code (tested with mediainfo.dll 0.7.4.1):

;use your own paths here
PathToMediaInfoDLL := "C:\Documents and Settings\Administrator\My Documents\autohotkey\MediaInfo.dll"
PathToAVIFile := "C:\Documents and Settings\Administrator\My Documents\autohotkey\6x09.avi"

hModule := DllCall("LoadLibrary", "str", PathToMediaInfoDLL)  ; Avoids the need for subsequent DllCalls to load the library

;get info about parameters, etc. This time we use ANSI (MediaInfoA_) version of the function
Info_ParametersPtr:=DllCall("mediainfo\MediaInfoA_Option", "UInt", 0, "str", "Info_Parameters", "str", "") ;see also Info_Parameters_CSV
VarSetCapacity(Info_Parameters, 30000) ;prepare to hold large data
Info_Parameters:=ExtractData(Info_ParametersPtr) ;dll returns a pointer, we build a string from that
;clipboard:=Info_Parameters ;easier to read
MsgBox "MediaInfo_Option - Info_Parameters"`n`n%Info_Parameters%

Info_CapacitiesPtr:=DllCall("mediainfo\MediaInfoA_Option", "UInt", 0, "str", "Info_Capacities", "str", "")
VarSetCapacity(Info_Capacities, 30000)
Info_Capacities:=ExtractData(Info_CapacitiesPtr)
MsgBox "MediaInfo_Option - Info_Capacities"`n`n%Info_Capacities%

Info_CodecsPtr:=DllCall("mediainfo\MediaInfoA_Option", "UInt", 0, "str", "Info_Codecs", "str", "")
VarSetCapacity(Info_Codecs, 30000)
Info_Codecs:=ExtractData(Info_CodecsPtr)
MsgBox "MediaInfo_Option - Info_Codecs"`n`n%Info_Codecs%

handle := dllcall("mediainfo\MediaInfo_New") ;initialize mediainfo

GetUnicodeString(PathToAVIFileUni,PathToAVIFile) ;we translate path to unicode, as we're gonna call unicode (MediaInfo_) version of the function
resultopenfile := dllcall("mediainfo\MediaInfo_Open", "UInt", handle, "str", PathToAVIFileUni) ;open the file
msgbox errorlevel %errorlevel%, resultopenfile %resultopenfile% ;if resultopenfile=1 -> success


;some examples

;Inform with Complete=false
DllCall("mediainfo\MediaInfoA_Option", "UInt", 0, "str", "Complete", "str", "") ;set the last "" to "1" for complete=true
capac:=VarSetCapacity(inform, 30000)
inform := dllcall("mediainfo\MediaInfo_Inform", "UInt", handle, "int", 0) ;we use unicode version of the function
informANSI := GetAnsiStringFromUnicodePointer(inform) ;translate unicode to ansi
msgbox errorlevel %errorlevel%, inform:`n`n%informANSI%

;Get with Stream=General and Parameter=AudioCount
Info_GetPtr:=DllCall("mediainfo\MediaInfoA_Get", "UInt", handle, "int", 0, "int", 0, "str", "AudioCount", "int", 1, "int", 0)
Info_Get := ExtractData(Info_GetPtr)
msgbox Get with Stream=General and Parameter=AudioCount`n%Info_Get%

;Get with Stream=video and Parameter=FrameRate
FrameRatePtr:=DllCall("mediainfo\MediaInfoA_Get", "UInt", handle, "int", 1, "int", 0, "str", "FrameRate", "int", 1, "int", 0)
FrameRate := extractdata(FrameRatePtr)
msgbox get with stream=general and parameter=FrameRate`n%FrameRate%


handle := DllCall("mediainfo\MediaInfo_Delete", "UInt", handle) ;Delete MediaInfo handle

DllCall("FreeLibrary", "UInt", hModule)  ; To conserve memory, the DLL may be unloaded after using it.


;credits for this function go to PhiLho (http://www.autohotkey.com/forum/topic12460.html)
; Some API functions require a WCHAR string.
GetUnicodeString(ByRef @unicodeString, _ansiString)
{
   local len

   len := StrLen(_ansiString)
   VarSetCapacity(@unicodeString, len * 2 + 1, 0)

   ; http://msdn.microsoft.com/library/default.asp?url=/library/en-us/intl/unicode_17si.asp
   DllCall("MultiByteToWideChar"
         , "UInt", 0             ; CodePage: CP_ACP=0 (current Ansi), CP_UTF7=65000, CP_UTF8=65001
         , "UInt", 0             ; dwFlags
         , "Str", _ansiString    ; LPSTR lpMultiByteStr
         , "Int", len            ; cbMultiByte: -1=null terminated
         , "UInt", &@unicodeString ; LPCWSTR lpWideCharStr
         , "Int", len)           ; cchWideChar: 0 to get required size
}

;credits for this function go to PhiLho (http://www.autohotkey.com/forum/topic12460.html)
; Some API functions return a WCHAR string.
GetAnsiStringFromUnicodePointer(_unicodeStringPt)
{
   local len, ansiString

   len := DllCall("lstrlenW", "UInt", _unicodeStringPt)
   VarSetCapacity(ansiString, len, 0)

   DllCall("WideCharToMultiByte"
         , "UInt", 0           ; CodePage: CP_ACP=0 (current Ansi), CP_UTF7=65000, CP_UTF8=65001
         , "UInt", 0           ; dwFlags
         , "UInt", _unicodeStringPt ; LPCWSTR lpWideCharStr
         , "Int", len          ; cchWideChar: size in WCHAR values, -1=null terminated
         , "Str", ansiString   ; LPSTR lpMultiByteStr
         , "Int", len          ; cbMultiByte: 0 to get required size
         , "UInt", 0           ; LPCSTR lpDefaultChar
         , "UInt", 0)          ; LPBOOL lpUsedDefaultChar

   Return ansiString
}

;credits for this function go to Goyyah (http://www.autohotkey.com/forum/viewtopic.php?p=91578#91578)
ExtractData(pointer) {
Loop {
       errorLevel := ( pointer+(A_Index-1) )
       Asc := *( errorLevel )
       IfEqual, Asc, 0, Break ; Break if NULL Character
       String := String . Chr(Asc)
     }
Return String
}

It's an adaptation of an example AutoIt script which can be found here: http://sourceforge.n...forum_id=297610. You may want to check this one out as well, as mine is only a subset of the AutoIt one.

I'm just starting with AHK, so feel free to correct my code if you think it can be improved.

Cheers,

mprost

Dippy46
  • Members
  • 171 posts
  • Last active: Aug 17 2007 06:53 AM
  • Joined: 06 Jul 2004
@mprost

Questions are always being asked about how to work with DLL's . Learning by example has in my opinion to be the best method.
This is an EXCELLENT example that you have produced and should be a guide to other people who want to learn about DLL's

I haven't the time to test the code but I think this DLL will provide me with some of the fuctionality I've been looking for. Nice find

Regards

Dave.
Simple ideas lie within reach, only of complex minds

dreadycarpenter
  • Members
  • 8 posts
  • Last active: Jan 15 2015 06:13 PM
  • Joined: 05 Jan 2007
hello, I'm fairly new to AHK , so I am having a hard time understanding how to use DllCall to retrieve mediainfo.

Could someone give me a simple example how to get these as a var:
Video Format
Video Bitrate
Audio Format
Audio Bitrate
Resolution
Framerate
Filesize

here is the script I would like to add dllcall to retrieve info for each avi
movdir=v:\newmovies
Loop, %movdir%\*.avi, 0, 0
{
;DllCall?? to get VidFormat, VidBitRate, AudFormat, AudBitRate, Res, FrameRate, FileSize
FileAppend, `r`n%VidFormat%`;%VidBitRate%`;%AudFormat%`;%AudBitRate%`;%Res%`;%FrameRate%`;%FileSize%,AmcImport.csv
}
I have posted a deeper explanation here, but no replies yet.

thanks to anyone that can help...
Cheers,

littlebut0
  • Members
  • 49 posts
  • Last active: Sep 26 2010 07:16 PM
  • Joined: 22 Dec 2005
Hi,

I've posted a reply to your question yesterday (see your other topic http://www.autohotke...5462&highlight=
Hope this can help solve your problem

Littlebut0

BoBo¨
  • Guests
  • Last active:
  • Joined: --
Would that allow to extract generic info (like artist/titel/track/duration) from an online stream like Last.FM or Shoutcast ??

Had a try already but not shure if it's not working because of it's not possible or I'm to stupid (TBH, I tend to that reason). :(

mprost
  • Members
  • 5 posts
  • Last active: Jun 02 2014 03:25 PM
  • Joined: 15 Dec 2006
Sorry for answering so late.

I don't think that is possible (http://mediainfo.sou...Support/Formats), but I don't know a lot about this DLL. Maybe you can find more guidance at the mediainfo forums.

mprost

hermes as guest
  • Guests
  • Last active:
  • Joined: --
I would love it if you could just script a simple load of inputs to get the following music tags out of mp3 files:

artist
album
track
tracknumber