从 jq.exe 输出中获取变量 Windows 批量读取 JSON



我设法拥有一个由ffprobe创建的JSON文件,其中包含有关MKV容器中视频流的基本信息。通过jq-win64.exe "[.format.duration]" %%~ni.mkv.json,可以从文件中正确读取电影的持续时间,并且jq回显["1:36:55.184000"]。现在我想将此值存储在脚本的全局变量中以供进一步处理。我尝试了几种方法,但每种方法都会导致错误和/或留空%duration%。我尝试了例如

for %%i in (*.mkv) do (
SETLOCAL ENABLEDELAYEDEXPANSION
for /F "tokens=* USEBACKQ" %%F IN ('_toolsjqjq-win64.exe "[.format.duration]" %%~ni.mkv.json') DO (SET duration=%%F)
echo Duration is: %duration%
ENDLOCAL
) 

但无法呼应%duration%.我认为这不可能那么难,很可能我没有在 Windows 批处理上正确执行语法。有什么想法吗?这也是 JSON 文件:

{
"format": {
"filename": "TestFile_1080p_26Mbs_8bit_BT709.mkv",
"nb_streams": 1,
"nb_programs": 0,
"format_name": "matroska,webm",
"format_long_name": "Matroska / WebM",
"start_time": "0:00:00.000000",
"duration": "1:36:55.184000",
"size": "17.586597 Gibyte",
"bit_rate": "25.978148 Mbit/s",
"probe_score": 100,
"tags": {
"title": "TestFile",
"encoder": "libmakemkv v1.14.4 (1.3.5/1.4.7) win(x64-release)",
"creation_time": "2019-08-17T21:01:18.000000Z"
}
}
}

这是一个基于我在评论后的理解的批处理文件解决方案:

For /F Tokens^=2Delims^=^" %%F In (
'_toolsjqjq-win64.exe "[.format.duration]" "%%~ni.mkv.json" 2^>NUL')Do (
Set "duration=%%F"
SetLocal EnableDelayedExpansion
Echo( !duration!
EndLocal
)

如果你想要的只是持续时间,那么就不需要中间的JSON,因为FFprobe也可以告诉你:

ffprobe.exe -v 0 -i <input> -show_entries format=duration -of compact=p=0:nk=1
1:36:55.184000

创建一个变量:

FOR /F "delims=" %%A IN (
'ffprobe.exe -v 0 -i <input> -show_entries format=duration -of compact=p=0:nk=1'
) DO SET duration=%%A
SET duration=1:36:55.184000

如果你仍然想解析FFprobe的JSON,那么也不需要创建json文件,因为你可以简单地通过管道将其传输到JQ:

ffprobe.exe -v 0 -i <input> -show_format -of json | jq.exe -r .format.duration
1:36:55.184000

创建一个变量:

FOR /F "delims=" %%A IN (
'ffprobe.exe -v 0 -i <input> -show_format -of json ^| jq.exe -r .format.duration'
) DO SET duration=%%A
SET duration=1:36:55.184000

最新更新