How to get video file metadata (Media Created Date, Frame Rate, Frame Width)?

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
k0stell0
Posts: 21
Joined: 30 Oct 2022, 18:01

How to get video file metadata (Media Created Date, Frame Rate, Frame Width)?

08 Apr 2024, 22:33

Hi everyone,

I wonder if there is a way to get video file metadata such as Media Created Date, Frame Rate, and Frame Width, using Autohotkey.
I found FileGetAttrib function, but it has a limited number of attributes it can read.

Any help would be highly appreciated 🙏
XMCQCX
Posts: 231
Joined: 14 Oct 2020, 23:44

Re: How to get video file metadata (Media Created Date, Frame Rate, Frame Width)?

09 Apr 2024, 00:00

It can be done with "ExifTool" by Phil Harvey.
https://exiftool.org/

Json.ahk by thqby, HotKeyIt
https://github.com/thqby/ahk2_lib/blob/master/JSON.ahk

Code: Select all

#Requires AutoHotkey v2.0
#SingleInstance
#include <v2\JSON\JSON 1.0.7> 

fPathVideo := 'Insert Path to Video file'
fPathJson := A_ScriptDir '\output.json'

RunWait(A_ComSpec ' /c ""exiftool" -j `"' fPathVideo '`" > output.json"',, 'Hide')
contents := JSON.parse( FileOpen(fPathJson, 'r', 'UTF-8').Read() , keepbooltype := false, as_map := true)

MsgBox(
    'Duration: ' contents[1]['Duration'] '`n'
    'ImageSize: ' contents[1]['ImageSize']
)
k0stell0
Posts: 21
Joined: 30 Oct 2022, 18:01

Re: How to get video file metadata (Media Created Date, Frame Rate, Frame Width)?

09 Apr 2024, 03:58

Thanks a lot for your reply, @XMCQCX
Looks like ExifTool can do what I need.

I've been trying to modify your code to avoid using JSON and saving the output file, but I run into some issues.
I managed to get the required parameters and also found RunWaitOne function that is supposed to be returning the output without saving it to a file.
Here's my code:

Code: Select all

exifTool := A_ScriptDir '\Lib\exiftool.exe'
videoFile := A_ScriptDir '\Test.mp4'
Command := exifTool ' -s -s -s -MediaCreateDate -VideoFrameRate ' videoFile
Output := RunWaitOne(command)
MsgBox(Output)

RunWaitOne(command) {
    shell := ComObject("WScript.Shell")
    exec := shell.Exec(A_ComSpec ' /c ' command)
    return exec.StdOut.ReadAll()
}

I have two issues with this code:
1. It opens up CMD window and freezes the code until I manually close it.
2. I get an empty output.

Am I missing something?
Last edited by k0stell0 on 09 Apr 2024, 16:28, edited 1 time in total.
garry
Posts: 3771
Joined: 22 Dec 2013, 12:50

Re: How to get video file metadata (Media Created Date, Frame Rate, Frame Width)?

09 Apr 2024, 08:41

example , run hidden

Code: Select all

#Requires Autohotkey v2.0
A_Clipboard:=""
PR:=a_scriptdir . "\exiftool.exe"
F1:=a_scriptdir . "\test.mp4"
RunWait(A_ComSpec " /c " pr " `"" f1 "`" | clip", , "hide")
for x,y in strsplit(A_Clipboard, "`n", "`r")
  {
if InStr(y, "Image width")
  iw:=y
if InStr(y, "Video Frame rate")
  vf:=y
if InStr(y, "Media Create Date")
  cd:=y
  }
MsgBox("WIDTH=" iw "`nFRAME Rate=" vf "`nCREATE DATE=" cd)
ExitApp()
XMCQCX
Posts: 231
Joined: 14 Oct 2020, 23:44

Re: How to get video file metadata (Media Created Date, Frame Rate, Frame Width)?

09 Apr 2024, 11:29

Code: Select all

#Requires AutoHotkey v2.0
#SingleInstance

fPathVideo := 'test.mp4'
output := RunWaitOne('exiftool "' fPathVideo '"')

m := Map()

for _, line in StrSplit(output, '`n')
    if RegExMatch(line, '(.*?)\s*:\s*(.*)', &match)
        m[match[1]] := match[2]

MsgBox(
    'Image Width: ' m['Image Width'] '`n'
    'Video Frame Rate: ' m['Video Frame Rate']
)

RunWaitOne(command) {
    shell := ComObject("WScript.Shell")
    exec := shell.Exec(A_ComSpec ' /c ' command)
    return exec.StdOut.ReadAll()
}
Without the console flashing with RunCMD by SKAN viewtopic.php?p=448912#p448912

Code: Select all

#Requires AutoHotkey v2.0
#SingleInstance

#Include <v2\RunCMD\RunCMD>

fPathVideo := 'test.mp4'
output := RunCMD('exiftool "' fPathVideo '"')

m := Map()

for _, line in StrSplit(output, '`n')
    if RegExMatch(line, '(.*?)\s*:\s*(.*)', &match)
        m[match[1]] := match[2]

MsgBox(
    'Image Width: ' m['Image Width'] '`n'
    'Video Frame Rate: ' m['Video Frame Rate']
)
garry
Posts: 3771
Joined: 22 Dec 2013, 12:50

Re: How to get video file metadata (Media Created Date, Frame Rate, Frame Width)?

09 Apr 2024, 12:27

@XMCQCX thank you for the example used with CMDRET from @SKAN
I have no experience with RegEx and also with ahk V2 ...
k0stell0
Posts: 21
Joined: 30 Oct 2022, 18:01

Re: How to get video file metadata (Media Created Date, Frame Rate, Frame Width)?

09 Apr 2024, 17:40

Thanks a lot guys for the provided examples 🙏 Works exactly as I hoped
k0stell0
Posts: 21
Joined: 30 Oct 2022, 18:01

Re: How to get video file metadata (Media Created Date, Frame Rate, Frame Width)?

10 Apr 2024, 01:37

Hi @XMCQCX,
I was trying to take your code up a notch and use it to rename a branch of MP4 files in a folder. Naturally, I came across Loop Files, executing ExifTool for each file and then renaming the file. While it works, I found it somewhat slow, especially when a folder contains hundreds of files.
In search of a more optimal solution, I found that ExifTool can read metadata from all files in a folder in a single run by using the following command and it seems to be working as fast as reading a single file.

Code: Select all

exiftool -s -s -s -MediaCreateDate -VideoFrameRate -ext mp4 "D:\Test Folder"
The output looks like this:

Code: Select all

======== D:/Test Folder/VideoFile1.MP4
2024:03:27 04:22:20
50.00
======== D:/Test Folder/VideoFile2.MP4
2024:03:27 04:22:27
25.00
======== D:/Test Folder/VideoFile3.MP4
2024:03:27 04:22:48
29.97
======== D:/Test Folder/VideoFile4.MP4
2024:03:27 04:23:03
25.00
======== D:/Test Folder/VideoFile5.MP4
2024:03:27 04:23:15
25.00
    1 directories scanned
    5 image files read
I wonder what is the best way to process this output. I need to be able to search those parameters by file path via Loop Files.

Thanks again 🙏
garry
Posts: 3771
Joined: 22 Dec 2013, 12:50

Re: How to get video file metadata (Media Created Date, Frame Rate, Frame Width)?

10 Apr 2024, 05:21

EDIT : error , realised , this is NOT for MP4 ... its for pictures .... maybe can use it once ... (?)
example RENAME many pictures with irfanview ,
put pictures with Selma en Mieke in one folder > AA
create new folder depending created YEAR ... \BB\2018_Selma en Mieke

Code: Select all

;- copy photo's from AA to BB with YEAR\created date &original-name  , like  :
;- > ... \BB\2018_Selma en Mieke\2018_10_05 16_43_56_Selma en Mieke_DSC00285.jpg
;==============================
add:="Selma en Mieke"          ;- add text
extx:="jpg,png,bmp"            ;- extensions
SRCX:=a_scriptdir . "\AA"      ;- source folder
DEST:=a_scriptdir . "\BB"      ;- destination folder
;==============================
irfan:=a_programfiles . "\IrfanView\i_view64.exe"
if !FileExist(irfan)
 {
 Run("https://www.irfanview.com/")
 ExitApp()
 }
if !FileExist(srcx)
 {
 MsgBox("Create first the folder with pictures from persons " add " >`n" srcx, "", 262208)
 ExitApp()
 }
;===============================
Loop Files, srcx "\*.*"
  {
  x:=A_LoopFilePath
  SplitPath(x, &name, &dir, &ext, &name_no_ext, &drive)
  cx:=name_no_ext . "." . ext
  ab:=""
  if (ext ~= "^(?i:" RegExReplace(RegExReplace(extx,"[\\\.\*\?\+\[\{\|\(\)\^\$]","\$0"),"\s*,\s*","|") ")$")
    {
    ab:=" /convert=" . DEST . "\$E36868(%Y)_" . add . "\$E306_" . add . "_" . cx
    RunWait("`"" irfan "`" `"" x "`" " ab)
    }
  }
Run(dest)
return
;========================================================================  

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: kunkel321, TAC109, william_ahk and 55 guests