【问题标题】:batch file delete lines with same variable批处理文件删除具有相同变量的行
【发布时间】:2015-06-17 13:41:22
【问题描述】:

现在,我正在使用 ffmpeg 使用以下命令验证视频中的裁剪值:

ffmpeg -i "INPUT.mov" -vf "cropdetect=112:1:0" -f null NUL 2> "Crop_Values.txt"

输出被重定向到一个 TXT 文件,但大多数裁剪值是相同的,我尝试创建一个批处理文件来删除具有重复裁剪值的行。

您可以在下面找到 TXT 文件的一部分:

[Parsed_cropdetect_0 @ 00000000003b7f00] x1:624 x2:1297 y1:426 y2:675 w:672 h:240 x:626 y:432 pts:54 t:2.250000 crop=672:240:626:432
[Parsed_cropdetect_0 @ 00000000003b7f00] x1:624 x2:1297 y1:426 y2:675 w:672 h:240 x:626 y:432 pts:55 t:2.291667 crop=672:240:626:432
[Parsed_cropdetect_0 @ 00000000003b7f00] x1:624 x2:1297 y1:416 y2:675 w:672 h:256 x:626 y:418 pts:56 t:2.333333 crop=672:256:626:418
[Parsed_cropdetect_0 @ 00000000003b7f00] x1:0 x2:1297 y1:321 y2:937 w:1296 h:608 x:2 y:326 pts:57 t:2.375000 crop=1296:608:2:326
[Parsed_cropdetect_0 @ 00000000003b7f00] x1:0 x2:1362 y1:210 y2:937 w:1360 h:720 x:2 y:214 pts:58 t:2.416667 crop=1360:720:2:214
[Parsed_cropdetect_0 @ 00000000003b7f00] x1:0 x2:1919 y1:142 y2:937 w:1920 h:784 x:0 y:148 pts:59 t:2.458333 crop=1920:784:0:148
[Parsed_cropdetect_0 @ 00000000003b7f00] x1:0 x2:1919 y1:142 y2:937 w:1920 h:784 x:0 y:148 pts:60 t:2.500000 crop=1920:784:0:148
[Parsed_cropdetect_0 @ 00000000003b7f00] x1:0 x2:1919 y1:142 y2:937 w:1920 h:784 x:0 y:148 pts:61 t:2.541667 crop=1920:784:0:148

每一行都有与所分析的视频帧相关的信息(包括时间),但最后,我无法通过数千行来发现裁剪值何时发生变化。

所有与前一行具有相同crop= 值的行将被丢弃,我想保留整行(因为所有其他信息)。

这是我想出的批处理文件,通过使用来自其他类似脚本的信息:

@echo off
type NUL > New_Crop_Values.txt
for /f "tokens=* delims=" %%a in (Crop_Values.txt) do (
  findstr /irxec:".*crop.*" New_Crop_Values.txt > NUL || >> New_Crop_Values.txt echo.%%a
)

不幸的是,这似乎不起作用。我是一个新手,所以我不明白为什么 findstr 没有使用正则表达式根据字符串选择行。

【问题讨论】:

    标签: regex batch-file command-line ffmpeg line


    【解决方案1】:
    @echo off
    setlocal EnableDelayedExpansion
    
    set "lastCrop="
    (for /F "delims=" %%a in (Crop_Values.txt) do (
       set "line=%%a"
       if "!line:* crop=!" neq "!lastCrop!" (
          echo %%a
          set "lastCrop=!line:* crop=!"
       )
    )) > New_Crop_Values.txt
    

    【讨论】:

    • 感谢 Aacini 的回答。我更新了我的脚本以删除所有不包含“crop=”值的行,但它似乎不起作用。 code rem @echo off setlocal EnableDelayedExpansion SET INPUT=%~1 ffmpeg -i "%INPUT%" -vf "cropdetect=112:1:0" -t 5 -f null NUL 2> "%INPUT:~0,-4%_Crop.txt" & set "lastCrop=" (for /F "delims=" %%a in ('"%INPUT:~0,-4%_Crop.txt"') do ( set "line=%%a" if not "!line!" == "!line:crop=!" ( if "!line:* crop=!" neq "!lastCrop!" ( echo %%a set "lastCrop=!line:* crop=!" ) ))) > "%INPUT:~0,-4%_NewCrop.txt" 任何想法为什么?
    • 找到了!我的问题是我的一些输入文件有空格。通过使用useback 函数和for /f loop 解决了这个问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-12
    • 1970-01-01
    • 2012-11-17
    • 2020-04-12
    相关资源
    最近更新 更多