5

Directory contains 2 (or more) video files with any random names.

video1.mkv
video2.mkv

Need to find out duration of every video. To do that we use MediaInfo.

setlocal EnableDelayedExpansion

for %%a in (*.mkv) do (
  for /f "usebackq" %%a in (`"mediainfo --Inform=Video;%%Duration%% %%a"`) do set duration=%%a

  echo "!duration!" > "data.txt"
)

Problem is, it prints only 1 value/duration (I think for last file). It works, buy only for one file.

How do I make it work with all files present in directory?

3 Answers 3

3

just use >> instead of >, which appends instead of overriding

setlocal EnableDelayedExpansion

for %%a in (*.mkv) do (
  for /f "usebackq" %%a in (`"mediainfo --Inform=Video;%%Duration%% %%a"`) do set duration=%%a

  echo "!duration!" >> "data.txt"
)
Sign up to request clarification or add additional context in comments.

5 Comments

It prints two values now, but they are both duplicate of one same variable value of first or last file (out of two files total).
Well, perhaps it might be an idea not to try to use %%a as the metavariable in both for loops. If you were to use %%a and %%b for instance, you could echo %%a %%b>>"filename"
But %%a is a filename, I need to echo duration? I replaced second for to %%b but no change.
for /f "usebackq" %%b in ("mediainfo --Inform=Video;%%Duration%% %%a") do echo %%a %%b>>"filename" %%a is the filename, %%b the duration as returned by mediainfo - so there's no need to do the intermediate assignment to duration.
if you want an empty file before appending you must create the empty file first
1

If mediainfo.exe is somewhere in the path this name doesn't need to be quoted, but as the .mkv file names most likely do contain spaces change the quoting like this:

@Echo off&SetLocal EnableExtensions EnableDelayedExpansion
( For %%a in (*.mkv) do (
    Set "Duration="
    For /f "delims=" %%b in (
      'mediainfo --Inform^=Video^;%%Duration%% "%%a"'
    ) do set "Duration=%%b"
    echo !Duration!,"%%a"
  )
) > "data.txt"

It isn't of much use to have the duration without knowing which file it belongs to, so this sample run with *.mpg files has the file name appended.

2595000,"R:\video\Der Traum ihres Lebens - Das Erste 2010-07-09 20-15-00.mpg"
5333160,"R:\video\Dirty Harry 3 - Der Unerbittliche - RTL2 2010-05-29 00-10-00.mpg"
5651960,"R:\video\Die Spur des Falken - Das Erste 2010-05-28 00-40-00.mpg"

Comments

0

as long as MediaInfo is in %Path% the way I have been doing this is by the use of a temp text file.

for %%a in (*.mkv) do set mkv=%%a&& call :one

goto eof

:one
mediainfo "--Inform=General;%%Duration%%" "%mkv%" >> filetemp.txt

set /p dur= < filetemp.txt

echo %dur%
pause
del filetemp.txt
:eof

I use a variation of this to encode my TV shows with ffmpeg, using MediaInfo to get the duration, framerate, and frame height, from 1-15 shows I have recorded over the week.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.