batch file delete lines with same variable

149 Views Asked by At

Right now, I'm using ffmpeg to verify crop values in video using the command below:

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

The output is redirected to a TXT file, but most crop values are identical and I tried to create a batch file to delete the lines with duplicated crop values.

Below you can find a section of the TXT file:

[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

Each line has information pertinent to the video frame that was analyzed (including time), but at the end, I couldn't go through thousands of lines to discover when a change in crop values occurred.

All following lines with identical crop= value as the previous line would be discarded, and I would like to retain the whole line (because of all the other information).

This is the batch file I came up with, by using information from other similar scripts:

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

Unfortunately, this does not seem to work. I'm quite a newbie, so I couldn't understand why findstr did not select the lines based on the string using regex.

1

There are 1 best solutions below

2
On
@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