I have this script that I'm trying to write that finds video files in a dir, automatically removes black bars and transcodes to x265...
@echo off
setlocal enabledelayedexpansion
REM Loop through all video files in the current directory
for %%F in (*.mp4, *.avi, *.mkv, *.ts) do (
echo Processing "%%F"
REM Use FFMPEG to detect crop values
for /f "tokens=*" %%a in ('ffmpeg -i "%%F" -vf "cropdetect" -f null - 2^>^&1 ^| find /i "crop="') do (
set cropfilter=%%a
)
REM Extract the crop filter result
set cropfilter=!cropfilter:*crop=!
set cropfilter=!cropfilter: =!
REM Use the detected crop filter to crop the video
if not "!cropfilter!"=="" (
echo Detected crop filter: !cropfilter!
ffmpeg -i "%%F" -map 0:v:0 -map 0:a:0 -map 0:s? -c:v:0 libx265 -crf 18 -vf "crop=!cropfilter!" -c:a:0 aac -ac 2 -b:a:0 256k -c:a:1 aac -ac 6 -b:a:1 512k -c:s copy "encoded.mkv"
) else (
echo No cropping needed for "%%F".
)
)
echo Processing complete.
pause
I'm struggling to correctly parse the crop values output from the initial ffmpeg query though. What its grabbing at the moment is:
detect_0@000001ce4b0e2040]x1:0x2:1919y1:131y2:948w:1920h:816x:0y:132pts:625483604t:6949.817822limit:0.094118crop=1920:816:0:132
When I really just need this bit:
crop=1920:816:0:132
So that I can call it in the transcode command.
Thanks in advance.
Having set
cropfilterto nothing, I'd suggest the following mods to your code:Use
set "var=value"for setting string values - this avoids problems caused by trailing spaces. Don't assign"or a terminal backslash or Space. Build pathnames from the elements - counterintuitively, it is likely to make the process easier. If the syntaxset var="value"is used, then the quotes become part of the value assigned.Note : your line
will remove all characters up to and including
cropyielding=1920:816:0:132AND the code line contains a terminal space, so that space will be included in the value
set.So - the replacement line will replace all characters up to and including
cropwithcropand there is no terminal space, so the second line is probably not needed.