Page 1 of 1

Clean using ffmpeg

Posted: Sun Jun 23, 2024 7:57 pm
by millertinymouse
I was not happy with the cutting that was being done with the other methods so I explored using Ffmpeg.
I adapted the batch file and I think it is working well.
I have included it if anyone else in interested.
@echo off
Rem comclean5.bat Version 0.0.1
Rem this is based on comclean1.bat used with mpgtx, not sure whom to credit.
Rem Adapt to your framerate times 100 (PAL=2500/NTSC=2997)
set framerate=2997
Rem Adapt to where ffmpeg.exe can be found
set ffmpeg="C:\Program Files\NextPVR\other\ffmpeg.exe"
set vidfile='%~dpn1%~x1'
set cutfile="%~dpn1.ffcut"

rem Initialize the variables
set frameusestart=0
set framemax=
set cutpoints=

rem Process all the lines from the Comskip output file
for /F "usebackq tokens=1,2,3,4,5,6,7,8" %%i in ("%~dpn1.txt") do call :processline %%i %%j %%k %%l %%m %%n %%o %%p

echo test if need ending line %frameusestart% %framemax%
if "%framemax%"=="" goto :noendshow
set /a needend=(%frameusestart% - %framemax%)
if %needend% GTR 0 goto :noendshow
echo processing end of show line
call :writeline %frameusestart% %framemax%

:noendshow
echo Resulting cutpoints: %cutpoints%
rem pass the cutfile to ffmpeg
%ffmpeg% -y -f concat -safe 0 -i %cutfile% -c copy "%~dpn1_clean.mpg"
%ffmpeg% -y -fflags +genpts -i "%~dpn1_clean.mpg" -f dvd -vcodec copy -acodec copy "%~dpn1_clean2.mpg"
del "%~dpn1_clean.mpg"
goto :eof

:processline
set arg=%1
echo line: %1 %2
if "%arg:~0,4%" NEQ "FILE" goto :skipheader1
rem this is the first line which should contain maxframe and framerate
rem initialize the cut file with the version line.
echo ffconcat version 1.0 > %cutfile%
IF 4%4 NEQ +4%4 goto :skipheader2
echo framemax found %4
set framemax=%4

:skipheader2
IF 7%7 NEQ +7%7 goto :skipprocess
echo framerate found %7
if "%7"=="2500" set framerate=%7
if "%7"=="2997" set framerate=%7
if "%7"=="5994" set framerate=%7
goto :skipprocess

:skipheader1
if "%arg:~0,4%"=="----" goto :skipprocess
if "%1"=="0" goto :nostartshow
rem write a line with the start and stop, note these are opposite from the TXT file and in seconds not frames.
call :writeline %frameusestart% %1

:nostartshow
rem Remember start of next clip
set frameusestart=%2
:skipprocess
EXIT /B 0

:writeline
call :tosecs %1
set cutpoints=%cutpoints% %tim%
echo file %vidfile% >> %cutfile%
echo inpoint %tim% >> %cutfile%
call :tosecs %2
echo outpoint %tim% >> %cutfile%
EXIT /B 0

:tosecs
set tim=
set /A position=%1 *1000 / %framerate%
set /A psecs=%position%/10
set /A pfrac=%position% - (%psecs% * 10)
set tim=%psecs%.%pfrac%
EXIT /B 0

:eof