deleting the input file after handbraking in batch-file - batch-file

I wrote this code for HandBrakeCLI as a batch file to manipulate my videos. This code creates output files with input file name plus a "_conv" suffix.
for /R .\test %%F in (*.mov) do HandBrakeCLI -e x264 --x264-preset medium -q 35 --crop 0:0:0:0 --aencoder copy -i "%%~fF" -o "%%~pF%%~nF_conv.mp4
Then I want to delete the original file and then remove _conv part of the output file. What should be added to the code above?
I want to delete each file just after converting it, or at least when going from its containing folder to another folder, not wholly after converting all of the file (because lots of files must be converted and I may run out of space)
By the way, how can I add other formats in addition of *.mov in the code?

for /R .\test %%F in (*.mov) do (
HandBrakeCLI -e x264 --x264-preset medium -q 35 --crop 0:0:0:0 --aencoder copy -i "%%~fF" -o "%%~dpF%%~nF_conv.mp4"
if exist "%%~dpF%%~nF_conv.mp4" (
del "%%~fF"
ren "%%~dpF%%~nF_conv.mp4" "%%~nxF"
)
)
All the information is inside your original code. All that is needed is to wrap the set of commands in parenthesis so the three commands are executed for each of the input files. Also, an aditional if has been included to only delete the source file if the converted file exists.

Related

Output file names with spaces to file without quotes in batch

So, I wrote a script as a batch file that uses FFmpeg to "concat" several video files on my hard drive.
The script is as follows.
#echo off
title Printing video info...
(for %%i in (
"%USERPROFILE%"/Dropbox/Video1.MKV
"%USERPROFILE%"/Dropbox/Video2.MKV
S:/Exports/Video3.MKV
../../video/Video4.mkv
) do ( if exist "%%i" echo file '%%i' )) > "%~n0.txt"
type "%~n0.txt"
title Copying to compiled video...
"C:\Program Files\ffmpeg\bin\ffmpeg.exe" -hide_banner -f concat^
-safe 0 -y -i "%~n0.txt" -c copy "%~n0.mkv"
The problem here is the username on the computer has a space in the name, so the script doesn't work. If I put the quotes with %USERPROFILE%, then the file is detected by the batch script, but the batch script also puts the quotes into the output TXT file, which causes FFmpeg to fail when it hits that file.
The contents of the text file the script outputs to should be:
file 'C:\Users\Name/Dropbox/Video1.MKV'
file 'C:\Users\Name/Dropbox/Video2.MKV'
file 'S:/Exports/Video3.MKV'
file '../../video/Video4.mkv'
Quote all the file path and apply the ~ modifier properly to %%i as follows:
(for %%i in (
"%USERPROFILE%/Dropbox/Video1.MKV"
"%USERPROFILE%/Dropbox/Video2.MKV"
S:/Exports/Video3.MKV
../../video/Video4.mkv
) do ( if exist "%%~i" echo file '%%~i' )) > "%~n0.txt"

Batch attachment of multiple .ttf files to a .mkv

Ok, I'm total new at this... Basically I'm using a tool call mkvmerge to attach multiple font files(.ttf) to .mkv files. I have separated the .mkv files into folders together with the respective fonts I would like to attach.
My aim is to create a batch that creates a copy of all the .mkv files with all the added attachments and deposits them in a newly created a folder (i.e Revised) in the parent directory.
Beginning with just a single folder:
mkdir Revised
for %%A in (*.mkv) do "%mkvmerge%" -q -o "Revised\%%A" "%%A" --attachment-mime-type application/x-truetype-font --attach-file "%%~.ttf"
This works if I change the "%%~.ttf" to an actual .tff file name
(i.e
mkdir Revised
for %%A in (*.mkv) do "%mkvmerge%" -q -o "Revised\%%A" "%%A" --attachment-mime-type application/x-truetype-font --attach-file "sans serif.ttf"
and I would end up with newly created Revised folder which contains a .mkv file with the sans serif.tff file attach within the .mkv file itself.
However I would like to add multiple .ttf files without naming them individually. (searching online it seems I need something like "$file" though I dont know how to use it)
Next if I have a parent folder with multiple sub-folders:
mkdir Revised
for /R %%A in (*.mkv) do "%mkvmerge%" -q -o "Revised\%%A" "%%A" --attachment-mime-type application/x-truetype-font --attach-file "%%~.ttf"
This just flat out doesn't work. Not just because of the "%%~.ttf" issue I'm sure.
I know that it might be a bit too ambitious, so if some one could just help solve the first half of my problem, that would be lovely. Thanks a lot in advance.
Ps: If anyone need to understand the mkvmerge specific commands to help out: https://mkvtoolnix.download/doc/mkvmerge.html
Updates: For the first part
mkdir Revised
for %%x in (*.ttf) do (
for %%A in (*.mkv) do "%mkvmerge%" -q -o "Revised\%%A" "%%A" --attachment-mime-type application/x-truetype-font --attach-file "%%x"
)
It seems to work better but I think the script would now add and remove the the .ttf files until the last .ttf file in the folder remained.
Please give this a try. (Remember to set your %mkvmerge% variable to your executable path):
#echo off
set "mkvmerge=C:\Some Path\mkvmerge.exe"
for %%a in (*.ttf) do (
for /f %%i in ('dir /s /b /a-d *.mkv ^| findstr /vi Revised') do (
if not exist "%%~dpiRevised" mkdir "%%~dpi\Revised"
if not exist "%%~dpiRevised\%%~nxi" copy "%%~fi" "%%~dpiRevised"
"%mkvmerge%" -q -o "%%~dpiRevised\%%~ni_rev%%~xi" "%%~dpiRevised\%%~nxi" --attachment-mime-type application/x-truetype-font --attach-file "%%~dpi%%a"
)
)
So to explain what went wrong with your examples:
In the for loop, you take apply from the mkv inside the root folder, and apply a ttf file to it and create the new mkv file with the attached ttf to the Revised directory, then for the next ttf you again copy from the root directory, overwriting the mkv file in the Revised directory with a new one where a new ttf was applied etc.
Instead, we need to first make a copy of the mkv file into the Revised directory then we apply the first ttf file to itself in Revised and then take the mkv with the already attached ttf and apply another ttf to it until all ttf files have been applied to the new mkv inside of Revised The original mkv and all the ttf files will remain in the parent folder.
Note if any of what I explained does not make sense, let me know and I will rephrase.
I have decided to take a stab at this, it is intended to be run from the parent directory holding your directories, (I have assumed that those directories are all on the same level, this is not recursing through nested directories).
Please be aware that I am unable to test this.
#Echo Off
Set "mkvm=%UserProfile%\Downloads\Video Players, Editors, & Downloaders\MKVTool Batch Modifier\mkvmerge.exe"
For /F Delims^=^ EOL^= %%A In ('Dir/B/AD 2^>Nul^|FindStr/IVXC:"Revised"'
) Do If Exist "%%A\*.mkv" (If Exist "%%A\*.ttf" (
If Not Exist "Revised\" MD "Revised" 2>Nul||Exit /B
Call :S1 "%%A"))
GoTo :EOF
:S1
PushD %1 2>Nul||Exit /B
Set "as=--attachment-mime-type application/x-truetype-font --attach-file"
Set "ttfs="
For /F Delims^=^ EOL^= %%A In ('Where .:*.ttf'
) Do Call Set "ttfs=%%ttfs%% %as% "%%~nxA""
For /F Delims^=^ EOL^= %%A In ('Where .:*.mkv'
) Do "%mkvm%" -q -o "%~dp0Revised\%%~nxA" "%%~nxA" %ttfs%
PopD
I think I have also made this in such a way as to allow for more than one .mkv file in a directory, where each will be attached to all of the same .ttf files.
Get-ChildItem *.mkv | ForEach-Object {
$FontFlags = ""
Get-ChildItem -LiteralPath $_.BaseName -Exclude *.xml | ForEach-Object {
$FontFlags += " --add-attachment `"$($_.FullName)`""
}
Write-Host "Running &`"C:\Program Files\MKVToolNix\mkvpropedit.exe`" `"$_`" $FontFlags"
Invoke-Expression "&`"C:\Program Files\MKVToolNix\mkvpropedit.exe`" `"$_`" $FontFlags"
}
Read-Host "Press any key to exit..."
Make a folder with the same names as mkv without extension. Put mkv file, folder , this code in same folder and run it.
I didn't make this code. I asked someone to make this for my personal use.

Audio Conversion: Nesting loops in batch file gives no output

I'm converting a couple thousand .wem files to .wav format. Since .wem is an obscure file format, I've decided to convert the files with SoX.
The SoX standard installation includes a batch file for use with mass converting raw files, which I've happily used. I've modified the example to remove the need to drag and drop files onto the batch file, using nested loops.
cd %~dp0
mkdir converted
for %%f in (*.wem) do FOR %%A IN (%*) DO sox -t raw -e signed-integer -b 16 -c 1 -r 44100 %%A "converted/%%~nA.wav"
pause
It creates a /converted directory just fine, and then proceeds to output the command, the directory, and then moves on to (supposedly) the next file. Unfortunately, opening the /converted directory reveals that nothing was generated at all, and the conversion took a lot less time than it should have. I've tested it with .wem files prior, and it converts just fine, so I don't think it has anything to do with the format. More likely, it just has something to do with my amateur batch file programming. Any ideas?
Solved, thanks to Mofi.
Finished code:
cd %~dp0
mkdir converted
for %%I in (*.wem) do sox -t raw -e signed-integer -b 16 -c 1 -r 44100 "%%I" "converted\%%~nI.wav"
pause
Seems I really could do it in one loop.

How to batch convert .sph files to .wav with sox

I have a directory with many folders containing hundreds of .SPH files. I need to convert all .SPH files into .wav format. I have adopted the following code:
cd %~dp0
mkdir converted
FOR %%A IN (%*) DO sox -t raw -s -2 -r 22050 -c 2 %%A "converted/%%~nA.wav"
pause
However, it doesn't do anything on Windows 7. When I try the code on CMD inside a folder where some of .SPH are:
sox *.SPH output.wav
It embeds all *.SPH into output.wav file, which is not what I want. I need name1.SPH to name1.wav, name2.SPH to name2.wav
Please help.
For Linux consider this:
for f in *.SPH; do sox -t sph "$f" -b 16 -t wav "${f%.*}.wav"; done
In Windows 7, I need to provide the entire path for the destination files, and the mdkir command got a permission error even though I was running as Administrator, so pre-make your destination directory and delete the mkdir command, and then your SOX command should look similar to this (with your own flags based on the conversion or edit you are trying to accomplish):
FOR %%A IN (%*) DO sox -t raw -e mu-law -c 1 -r 8000 %%A c:\converted\%%~nA.wav
The cmd file should not be in the folder where the files are, it should be in the folder where SOX is installed, and then drag the files you want converted onto the cmd (or bat) file.
for %%a in (*.sph) do sox "%%~a" "%%~na.wav"
Make a .bat file in your sox directory with the contents
cd %~dp0
for %%a in (*.sph) do sox "%%~a" "%%~na.wav"
pause
Select and drag the files that you want to convert onto this .bat file and the .wav files with the same names as the .sph files will appear in the same folder as the .sph files.

Batch File For loop over a list of file extensions with exclusions

Say i have the following files in a directory
/file.js
/file2.min.js
/file1.js
how can i write a batch for loop such that all ".js" files are picked up but ".min.js" are not and the output of the .js filename can be changed to append .min.js
eg:
for %%A IN (*.js) DO #echo %%A "->" %%~nA ".min.js"
would ideally produce the following, and note the file2.min.js is not displayed to the left.
file.js -> file.min.js
file1.js -> file1.min.js
Thanks for your help.
Just look whether it already contains .min.js:
setlocal enabledelayedexpansion
for %%f in (*.js) do (
set "N=%%f"
if "!N:.min.js=!"=="!N!" echo %%f -^> %%~nf.min.js
)
Not that I disagree with #Joey's solution, but I thought it wouldn't hurt if I posted an alternative:
#ECHO OFF
FOR %%f IN (*.js) DO (
FOR %%g IN ("%%~nf") DO (
IF NOT "%%~xg" == ".min" ECHO "%%f" -^> "%%~g.min.js"
)
)
In the past, when I wanted to do something similar I used the renamex script. Here it is with examples:
Usage: renamex [OPTIONS] EXTENSION1 EXTENSION2
Renames a set of files ending with EXTENSION1 to end with EXTENSION2.
If none of the following options are provided, renaming is done in the current
directory for all files with EXTENSION1.
Where [OPTIONS] include:
-v --verbose print details of files being renamed
-d [directory]
--directory [full path] rename files specified in the directory
-f [filter]
--filter [wildcard]
use wildcard characters (* and ?) for renaming files
-h --help show this help
Examples:
renamex htm html
(rename all .htm files to .html in the current directory)
renamex -v log txt
(show verbose output while renaming all .log files to .txt)
renamex -v -d "D:\images" JPG jpeg
(rename all .JPG files located in D:\images to .jpeg)
renamex -v -d "D:\movies" -f *2011* MPG mpeg
(rename all .MPG files with 2007 in their names, in D:\movies to .mpeg)

Resources