简体   繁体   English

批次| 将多个变量回显到文件

[英]BATCH | echo multiple variables to file

Directory contains 2 (or more) video files with any random names. 目录包含2个(或更多)具有任意随机名称的视频文件。

video1.mkv
video2.mkv

Need to find out duration of every video. 需要找出每个视频的持续时间。 To do that we use MediaInfo . 为此,我们使用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). 问题是,它只打印1个值/持续时间(我认为是最后一个文件)。 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: 如果mediainfo.exe位于路径中的某个位置,则不需要引用此名称,但由于.mkv文件名很可能包含空格,因此更改引号如下:

@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. 在不知道它属于哪个文件的情况下持续时间没有多大用处,因此使用* .mpg文件运行的此示例具有附加的文件名。

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. 只要MediaInfo在%Path%中,我这样做的方法就是使用临时文本文件。

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. 我使用这个变体来编码我的电视节目与ffmpeg,使用MediaInfo来获取持续时间,帧速率和帧高度,从我在一周内记录的1-15个节目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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