downsizing multiple mpg files using ffmpeg.exe - batch-file

I'm using ffmpeg.exe to process screen captures for a demo using MS Game Bar. Game Bar captures at a high frame rate at high resolution and the files are very large.
Using ffmpeg.exe I can process the files using:
ffmpeg.exe -i file.mpg file_l.mpg
And all is good but I'd like to create a bat file so I can do half a dozen or any in a folder all at once.
My Windows bat skills are poor and I've been trying to get it to work using something like this without success:
for %%f in (*.mp4) do (
ren %%~nf%%~xf !fileNum!%%~xf
set/a fileNum += 1
Can anyone help please

here's one I've used that can perhaps be adapted to meet your needs.
SETLOCAL EnableDelayedExpansion
:: start loop
for %%G in (*.mp3) do (
:: get filename without extension
set OUTPUT=%%~nG
:: pass filename to FFmpeg output
ffmpeg -i %%G !OUTPUT!.wav
:: delete source file
del %%G
)
if your input format and output format are the same, you can do something like this:
SETLOCAL EnableDelayedExpansion
:: start loop
for %%G in (*.mp3) do (
:: get filename with extension
set MYNAME=%%~nxG
:: create temp name
ren !MYNAME! delete1.mp3
:: pass original filename to FFmpeg output
ffmpeg -i delete1.mp3 !MYNAME!
:: delete temp file
del delete1.mp3
)

Related

FFmpeg - Check if folder contains a matching filename with 2 different extensions and ignore both files. Process only filenames with 1 extension

I need to batch convert all mkv files in a folder recursively to mp4.
If a filename exists and matches both extensions, ignore both files and process only filenames that contain mkv, without matching mp4.
Example: cat.mkv exists in folder with cat.mp4 = ignore both files
Example: cat.mkv exists in folder and cat.mp4 does not = process cat.mkv to cat.mp4
I have included a script that doesn't work well. It processes all mkv files and mp4 files. The mp4 files throw an error as FFmpeg will not encode the same format in this manner over itself.
As always thank you to anyone who might have a few ideas.
UPDATE: I may have gotten it to work. I changed a few things from the original. If anyone has success or an idea to improve I'm all ears. Thanks.
VERSION 2
#ECHO ON
SETLOCAL
PROMPT $G
COLOR 0A
REM Set FFmpeg.exe location if not in system PATH already
SET FF=C:\MAB\local64\bin-video\ffmpeg.exe
REM Set MKV files root folder to recursively search
SET "mkvPATH=C:\Encoding\1_Original\Test\"
REM Change into mkvPATH DIR
CD "C:\Encoding\1_Original\Test"
REM Set temp file name
SET TEMPFILE=convert_mkv.bat
REM Create empty convert file
COPY NUL "%TEMPFILE%" >NUL 2>&1
REM ADD #ECHO OFF to top of blank convert_mkv.bat script
ECHO #ECHO OFF >>"%TEMPFILE%"
REM Recursively search MKV root folder
FOR /R "%mkvPATH%" %%G IN (*.mkv *.mp4) DO (
SET "GPATH=%%~fG"
SET "GNAME=%%~nG"
SETLOCAL ENABLEDELAYEDEXPANSION
REM Ignore all files that have both
REM extensions ".mkv" and ".mp4" in the file name
IF "%%~nG.mkv"=="%%~nG.mkv" (
IF NOT EXIST "%%~nG.mp4" (
CALL :DO_FFmpeg "!GPATH!"
IF "%%~nG.mkv"=="%%~nG.mkv" (
IF EXIST "%%~nG.mp4" (
ECHO(>>"%TEMPFILE%"
) ELSE ENDLOCAL
)
)
)
)
GOTO END
REM CALL variables for use in FFmpeg's command line
:DO_FFmpeg
IF "%~1"=="" GOTO :END
FOR %%I IN ("%~1") DO (
SET "FOLDER=%%~dpI"
SET "NAME=%%~nxI"
)
REM Export info to "%TEMPFILE% and RUN ffmpeg.exe's command line in the cmd.exe window
ECHO %FF% -y -i "%~1" -ss 0 -t 300 -codec copy "%FOLDER%%~n1.mp4">>"%TEMPFILE%" && %FF% | %FF% -y -i "%~1" -ss 600 -t 30 -codec copy "%FOLDER%%~n1.mp4"
:END
PAUSE
EXIT /B

Rename file as per barcode using windows cmd or batch script

I have some scan images which having barcode i had found cmd barcode scanner tool
bardecode -f C:\input\file1.png -t code128 >> C:\output\output.txt
this will read the image file and save barcode number in output.txt file.
I want to make a process so this command run on each file in a folder and rename that file with its barcode first 3 or 4 digits.
i try for loop, it runs the command on all png files in folder and print output in file.
for %%i in (C:\input\*.png) do bardecode -f %%i -t code128 >> C:\output\output.txt
but i want to use output to rename that file.
So, if I understood well, with no layout to test, I image the loop to do this.
If possible, please, provide barcode.exe output layout, without this, there no much references to perform test.
Please note that I need to know the layout of output.txt to improve my code:
#echo off && cd /d "%~dp0" & setlocal enableextensions enabledelayedexpansion
for /f "tokens=* delims= " %%i in ('dir /b /a-d C:\input\*.png') do (
for /f "tokens=*" %%I in ('"bardecode.exe" -f "!_name_!" -t code128') do (
set _full_name=%%~fi
set _name_only=%%~ni
set _bar_code_to_rename=%%I
set _bar_code_to_rename=!_bar_code_to_rename:~0,4!-!_name_only!
ren "!_full_name!" "!_bar_code_to_rename!.png"
)
)

Trying to trim and fade multiple wav files in remote folder with SOX

I'm trying to create a file so that when I drag and drop a selection of wav files onto the batch script, it a. copies the wavs to a subdirectory (/webclips), replaces the spaces with underscores, then tries to trim all of the new wav files from the beginning to 45 seconds out. Eventually I'll fade them out...
Right now the script is copying fine but then giving a couple errors when it runs the trim command:
sox WARN wav: Premature EOF on .wav input file
sox WARN trim: Last 2 position(s) not reached (audio shorter than expected).
The new wavs files are (incorrectly) 1Kb in size. Any help would be appreciated.
Here's my script
set FOLDERPATH=%~dp0
set FILEPATH=%~dp1
mkdir "%FILEPATH%webclips"
FOR %%A IN (%*) DO (copy %%A "%FILEPATH%webclips")
cd "%FILEPATH%webclips"
for %%j in (*.*) do (
set filename=%%~nj
set filename=!filename:.=_!
set filename=!filename: =_!
if not "!filename!"=="%%~nj" ren "%%j" "!filename!%%~xj"
)
c:
cd/
CD "Program Files (x86)\sox-14-4-2"
for %%A in ("%FILEPATH%webclips\*.wav") do sox "%%A" "%FILEPATH%webclips\%%~nxA" trim 0 45
pause
In last line, sox overwrote the searched .wav file. For sox, it cannot do like that.
So the operation of copying original .wav file to subfolder can be removed.
!variable! is delayed format and have to be declared setlocal enabledelayedexpansion before it.
the batch file can also be simplified as the following,
#echo off
setlocal enabledelayedexpansion
for %%a in (%*) do (
if not exist "%%~dpawebclips\" md "%%~dpawebclips\"
set file_name=%%~na
set file_name=!file_name: =_!
set file_name=!file_name:.=_!
ren %%a !file_name!%%~xa
"%ProgramFiles(x86)%\sox-14-4-2\sox.exe" "%%~dpa!file_name!%%~xa" "%%~dpawebclips\!file_name!%%~xa" trim 0 45
)
pause
exit /b

Using a .bat to extract text between two words

I have a media server and I'm attempting to automate ripping my collection of movies and any future movies using MakeMKV. Ripping and moving is working without a hitch. The problem I'm running into is occasionally MakeMKV doesn't assign a title to the MKVs and I end up with title00.mkv which Media Center Master obviously cannot even begin to try to match to any metadata.
MakeMKV does offer the ability to get the information from the disc which I have print to info.txt which looks like this.
MSG:1005,0,1,"MakeMKV v1.8.10 win(x64-release) started","%1 started","MakeMKV v1.8.10 win(x64-release)"
DRV:0,2,999,1,"HD-DVD-ROM HL-DT-ST BD-RE GGW-H20L YL05","FARFROMHOME_16X9","\\Device\\CdRom0"
DRV:1,256,999,0,"","",""
DRV:2,256,999,0,"","",""
DRV:3,256,999,0,"","",""
DRV:4,256,999,0,"","",""
DRV:5,256,999,0,"","",""
DRV:6,256,999,0,"","",""
DRV:7,256,999,0,"","",""
DRV:8,256,999,0,"","",""
DRV:9,256,999,0,"","",""
DRV:10,256,999,0,"","",""
DRV:11,256,999,0,"","",""
DRV:12,256,999,0,"","",""
DRV:13,256,999,0,"","",""
DRV:14,256,999,0,"","",""
DRV:15,256,999,0,"","",""
FARFROMHOME_16X9 is the label for the disc.
How can I extract this and rename my .mkv when makemkv has finished?
Here is my BAT so far (my first attempt at a .bat):
makemkvcon64 -r info disc > info.txt
makemkvcon64 --minlength=3600 mkv disc:0 all C:\Users\HTPC\MakeMKV_Temp\
START /WAIT makemkvcon64.exe
cd "c:\Program Files (x86)\FreeEject\"
FreeEject d:
move C:\Users\HTPC\MakeMKV_Temp\*.mkv C:\Users\HTPC\Movies\
Renaming the file before the move would be ideal.
Although you posted an ample description of your problem and data, you have not explained what exactly you want as result, so I must guess a couple points. The lines below extract the sixth comma-separated token from the second line of info.txt file:
for /F "skip=1 tokens=6 delims=," %%a in (info.txt) do set "discLabel=%%~a" & goto continue
:continue
echo The label of the disk is: %discLabel%
This is the output of previous lines when they are executed on your example data:
The label of the disk is: FARFROMHOME_16X9
You also have not indicated the name of the file you want to rename (before move it). Below is a possible solution to your problem that should be adjusted when previous unclear points be defined:
makemkvcon64 -r info disc > info.txt
for /F "skip=1 tokens=6 delims=," %%a in (info.txt) do set "discLabel=%%~a" & goto continue
:continue
makemkvcon64 --minlength=3600 mkv disc:0 all C:\Users\HTPC1\MakeMKV_Temp\
START /WAIT makemkvcon64.exe
cd "c:\Program Files (x86)\FreeEject\"
FreeEject d:
ren C:\Users\HTPC1\MakeMKV_Temp\TheNameHere.mkv %discLabel%.mkv
move C:\Users\HTPC1\MakeMKV_Temp\*.mkv C:\Users\HTPC1\Movies\
I've been wrangling with the same thing and created something that works fairly well.
MakeMKV uses it's own output FileName like you indicated "title00.mkv" or something similar.
I've created two(2) batch files to perform my home movie automation.
The first is "RipWorkflow_DVD.bat", which handles all the heavy lifting to call MakeMKV first to rip the content to my drive. Also in this file is a call to Handbrake to convert the MKV to MP4 (my stupid "smart" tv won't play MKV, but likes MP4)
The second batch file "OneStepDVDRip.bat" calls the first, but with parameters I choose for
a) location of the MKV
b) the output name of my MP4
c) the minLength of a track to locate and rip to MKV
Here is the code for "OneStepDVDRip.bat"
call "C:\Users\Todd\Desktop\RipWorkflow_DVD.bat" "Z:\Video\TrueStory" "E:\My Videos\Movies\Drama\True Story (2015).mp4" 3600
Here is the code for "RipWorkflow_DVD.bat"
#echo off
cls
setlocal ENABLEDELAYEDEXPANSION
SET input=%1
SET output=%2
SET minlength=%3
echo
echo %input%
echo %output%
echo
if not exist %input% ( mkdir "%input%" )
call "C:\Program Files (x86)\MakeMKV\makemkvcon64.exe" --minlength=%minlength% --decrypt mkv disc:0 0 "%input%
timeout /t 5 /nobreak
for %%F in (%input%\*.mkv) do (
echo %%~dpnxF
echo %output%
call "C:\Program Files\Handbrake\HandbrakeCLI.exe" -v1 -i %%~dpnxF --main-feature -o %output% -f mp4 --markers -e x264 -b 2000 -a 1 -E faac -6 dpl2 -R 44.1 -B 128 -D 1.75 --subtitle-forced -x ref=2:bframes=2:subme=6:mixed-refs=0:weightb=0:8x8dct=0:trellis=0
timeout /t 5 /nobreak
DEL %%F /Q
)
endlocal

Trying to use a batch file to work with filenames that have a forbidden character

Okay, a brief explanation of what I am doing: I use Windows Media Center (Windows 7) to record Jeopardy every evening. I then use Handbrake to convert the .wtv files to .mkv files and then transfer them to my NAS so I can watch them later using Plex Media Server/Center. Rather than doing this "by hand", I'm trying to automate the process using a batch file as a scheduled task. Initially, I had set up a script so that I could right-click > Send To > convert.bat and it would initiate the command-line interface for Handbrake and convert the file, move the output to my NAS, and delete the original file (worked great).
Now, what I'm doing is initiating the batch script as a scheduled task and looping through the contents of my "recorded tv" directory and looping through any .wtv files to convert/move/delete them.
The problem lies in the fact that Windows Media Center correctly names the Jeopardy files with the "!" in them (Eg: Jeopardy!_KHQ_2012_12_04_21_12_12.wtv), which completely bricks my script. The "Send To" batch file worked great, but when I loop through the *.wtv files in the directory, it returns all the filenames with the "!" stripped out which means I can't do squat with them. Files without "!" do process without a hitch.
Thanks in advance to anyone who can get me pointed in the right direction! (and if you happen to see any other areas where this script could be improved, that's fine too...)
Here is the basic code that I am attempting to use:
#echo off
setlocal ENABLEDELAYEDEXPANSION
SET count=0
SET getFolder=C:\Users\Public\Recorded TV\
SET ripFolder=C:\Rips\
SET putFolder=Z:\Videos\Recorded TV\
FOR %%F IN ("%getFolder%*.wtv") DO (
SET /A count=!count!+1
REM DETERMINE OUTPUT FILENAME
for /f "tokens=5,6,7,8,9,10 delims=\_" %%a in ("%%F") do (
set show=%%a
set station=%%b
set year=%%c
set month=%%d
set day=%%e
set hour=%%f
REM GENERATE OUTPUT NAMING CONVENTION
set output=!show! s!year!e!month!!day! !hour!
)
REM PROCESS WITH HANDBRAKE CLI
"C:\Program Files\Handbrake\HandBrakeCLI.exe" -i "%%F" -t 1 -c 1 -o %ripFolder%!OUTPUT!.mkv -f mkv --deinterlace="fast" --crop 58:60:2:2 --strict-anamorphic -e x264 -q 20 --vfr -a 1 -E faac -B 160 -6 dpl2 -R Auto -D 0 --gain=0 --audio-copy-mask none --audio-fallback ffac3 -x ref=1:weightp=1:subq=2:rc-lookahead=10:trellis=0:8x8dct=0 --verbose=1
REM MOVE CONVERTED FILE TO NAS
copy "%ripFolder%!OUTPUT!.mkv" "%putFolder%"
REM DELETE ORIGINAL
del "%%F"
REM DELETE LOCAL RIP
del "%ripFolder%!output!.mkv"
)
echo %count% files processed
pause
ENDLOCAL
As you have recognized, the exclamation mark is stripped before you can escape it.
That's because you expand the FOR-loop variable %%F while delayed expansion is enabled, and the exclamation mark tries to expand a variable.
You need to toggle the delayed expansion here, as the variable contents are safe when using with delayed expansion, but to get the value you need the disabled mode.
#echo off
setlocal DisableDelayedExpansion
SET count=0
FOR %%F IN ("%getFolder%*.wtv") DO (
set "orgFile=%%F"
SET /A count+=1
REM DETERMINE OUTPUT FILENAME
for /f "tokens=5,6,7,8,9,10 delims=\_" %%a in ("%%F") do (
set show=%%a
set station=%%b
set year=%%c
set month=%%d
set day=%%e
set hour=%%f
setlocal EnableDelayedExpansion
REM GENERATE OUTPUT NAMING CONVENTION
set output=!show! s!year!e!month!!day! !hour!
)
REM PROCESS WITH HANDBRAKE CLI
"C:\Program Files\Handbrake\HandBrakeCLI.exe" -i "%%F" -t 1 -c 1 -o %ripFolder%!OUTPUT!.mkv -f mkv --deinterlace="fast" --crop 58:60:2:2 --strict-anamorphic -e x264 -q 20 --vfr -a 1 -E faac -B 160 -6 dpl2 -R Auto -D 0 --gain=0 --audio-copy-mask none --audio-fallback ffac3 -x ref=1:weightp=1:subq=2:rc-lookahead=10:trellis=0:8x8dct=0 --verbose=1
REM MOVE CONVERTED FILE TO NAS
copy "%ripFolder%!OUTPUT!.mkv" "%putFolder%"
REM DELETE ORIGINAL
del "!orgFile!"
REM DELETE LOCAL RIP
del "%ripFolder%!output!.mkv"
endlocal
)

Resources