简体   繁体   中英

BATCH | echo multiple variables to file

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?

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"
)

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"

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM