batch %%~n1 not returning variable - batch-file

I have a script for converting video using a batch and ffmpeg. however im confused as to why %%~nf and %%~n1 aren't returning the name of the file (without the format). I've tried various " ' % to see what the cause is, though im quite lost as the %~1 variable works fine.
if not %Vcodec% == h264 if not %Acodec% == ac3 (echo Converting Video, Converting Audio
'C:\ffmpeg\bin\ffmpeg.exe -i "%~1" -map 0 -vcodec libx264 -scodec copy -acodec ac3 -b:a 640K '%%~n1.mkv')
Produces
C:\Users\Desktop\to fix>if not mpeg4 == h264 if not mp3 == ac3 (
echo Converting Video, Converting Audio
'C:\ffmpeg\bin\ffmpeg.exe -i "S02E01.avi" -map 0 -vcodec libx264 -scodec copy -acodec ac3 -b:a 640K %~n1.mkv'
)
rather than SO2EO1. if i add another % to it, it returns
C:\Users\Desktop\to fix>if not mpeg4 == h264 if not mp3 == ac3 (
echo Converting Video, Converting Audio
'C:\ffmpeg\bin\ffmpeg.exe -i "S02E01.avi" -map 0 -vcodec libx264 -scodec copy -acodec ac3 -b:a 640K %S02E01.mkv'
)
So i don't understand why one variable works and the other doesn't event though it should? As in the example I've used to make this script "%%~nf".mkv works. I'm a novice to scripting, so if you do provide an answer an explanation or a link to an explanation would be great.

%1 is an internal variable, so
'%~n1.mkv'
See https://ss64.com/nt/syntax-args.html
%~n1 Expand %1 to a file Name without file extension C:\utils\MyFile
or if only a path is present (with no trailing backslash) - the last
folder in that path.

Related

keep input name in ffmpeg?

can someone help me to keep original name in input as output
For example, i have a "Video.mp4" i need to convert it in mp3 as "Video.mp3"
but i cant find it how to do it!
i try
ffmpeg -i video.mp4 -b:a 192K -vn music.mp3
but the output file is "music.mp3" and i dont want it
soo i try ffmpeg -i A.mp4 -b:a 192K -vn *.mp3
and it gives error like :
*.mp3: Invalid argument
pls help me :(
[untried]
#echo off
ffmpeg -i "%~1" -b:a 192K -vn "%~n1.mp3"
As a batch file named tomp3.bat, use as tomp3 "video.mp4"
The quotes are not required unless the filename contains spaces (or some other symbols)

Batch file to perform multiple conversions in a row

I'm trying to write a batch file that can be launched from Handbrake GUI with the "send file to" feature, that will split an MKV into chapters with MKVmerge, then convert all those new files into mp4s with ffmpeg. Handbrake passes only one argument into the batch file, the full file path of the output (surrounded by quotes). I'm very new to batch scripting and am having difficulty getting it all to work together -- all the individual parts work just fine when absolute paths are given. Here's the content of the .bat file:
START /WAIT "C:\Program Files\MKVToolNix\mkvmerge.exe -o output.mkv --split chapters:all %1"
for %%i in (*.mkv) do C:\ffmpeg\bin\ffmpeg.exe -i "%%i" -loop 1 -i "%%~dpi"\folder.jpg -map 1:v -map 0:a -c:a ac3 -b:a 640K -pix_fmt yuv420p -c:v libx264 -shortest -fflags +shortest -max_interleave_delta 100M "%%~ni.mp4"
Any glaring mistakes I'm making? I've been at this for hours reading SO threads and documentation, and can't figure it out for the life of me. Any help is appreciated, thanks in advance.

Batch Script to Download Video with youtube-dl and Convert with FFmpeg

I'm trying to
Using a single batch script to
Download an mp4 video using youtube-dl
Save the video's original title to a batch variable
Convert the video to webm with FFmpeg
I want to keep the original title, so it needs to read the title with youtube-dl, save it to a variable, and use that variable for FFmpeg input/output filename.
CMD Batch
1. Download Video
youtube-dl -f best "https://www.youtube.com/watch?v=TWNhqCHw0qc" -o "C:\Users\Matt\Downloads\%%(title)s.mp4" --merge-output-format mp4
2. Download Video using Loop
This is used to save the title to a variable %%a.
for /f "delims=" %%a in ('youtube-dl -f best "https://www.youtube.com/watch?v=TWNhqCHw0qc" -o #"C:\Users\Matt\Downloads\%%(title)s.mp4" --merge-output-format mp4') do (echo example)
3. Final Script
Download Video, Save Title, Convert with FFmpeg
Sorted
for /f "delims=" %%a in ('
youtube-dl
-f best "https://www.youtube.com/watch?v=TWNhqCHw0qc"
-o #"C:\Users\Matt\Downloads\%%(title)s.mp4"
--merge-output-format mp4
')
do (ffmpeg -y
-i "C:\Users\Matt\Downloads\%%a.mp4"
-c:v libvpx -b:v 1300K -crf 16 -pix_fmt yuv420p
-map 0:v:0? -sn
-c:a libvorbis -q:a 6 -ac 2 -map 0:a:0?
-f webm
"C:\Users\Matt\Downloads\%%a.webm"
)
Inline
for /f "delims=" %%a in ('youtube-dl -f best "https://www.youtube.com/watch?v=TWNhqCHw0qc" -o #"C:\Users\Matt\Downloads\%%(title)s.mp4" --merge-output-format mp4') do (ffmpeg -y -i "C:\Users\Matt\Downloads\%%a.mp4" -c:v libvpx -b:v 1300K -crf 16 -pix_fmt yuv420p -map 0:v:0? -sn -c:a libvorbis -q:a 6 -ac 2 -map 0:a:0? -f webm "C:\Users\Matt\Downloads\%%a.webm")
Error
Before the script can ever reach FFmpeg, youtube-dl fails to download the file. It says the file has already been downloaded, even when there is no file in the directory.
[download] #C#\Users\Matt\Downloads\Color Balloons.mp4 has already
been downloaded
I was able to create a script that works.
One of the problems was that youtube-dl wasn't working properly if located in C:\Program Files\, but works when located somewhere that doesn't require Administrator Privileges.
The --get-filename part requires an # symbol before the youtube-dl path.
youtube-dl gets filename from video and saves to var %%f
youtube-dl downloads the video to C:\Path\To\Downloads\
Merge output as mp4, so you can specify the extension for FFmpeg input
FFmpeg converts video to webm and uses %%f as input/output filename.
for /f "delims=" %%f in ('
#"C:\Users\Matt\Desktop\youtube-dl.exe"
--get-filename -o "%%(title)s"
"https://www.youtube.com/watch?v=TWNhqCHw0qc"
')
do ("C:\Users\Matt\Desktop\youtube-dl.exe"
-f best "https://www.youtube.com/watch?v=TWNhqCHw0qc"
-o "C:\Users\Matt\Downloads\%%f.mp4"
--merge-output-format mp4
&&
ffmpeg -y
-i "C:\Users\Matt\Downloads\%%f.mp4"
-c:v libvpx
-b:v 1300K -crf 16
-pix_fmt yuv420p
-map 0:v:0?
-sn
-c:a libvorbis
-q:a 6
-map 0:a:0?
-f webm
"C:\Users\Matt\Downloads\%%f.webm"
)

Bulk converting MKV files to MP4 with AAC using a Windows batch file

So I have been trying for days to work out how to do this and this site has helped me but I am still stuck.
I basically want to run a batch file and it goes through a folder including all subdirectories and changes the container of all mkv files to mp4 converting the sound to AAC if necessary. Preferably it will create the new mp4 file in the same place with the same name and then delete the mkv.
All I have so far is this which if I place it in a certain folder will create the mp4's in a specified folder called newfiles. I am not sure if I have used libfaac correctly either. Would greatly appreciate any advice. I would have liked to use libfdk_aac but I have tried everything to get it and I cant find it.
for %%a in ("*.mkv") do C:\ffmpeg\bin\ffmpeg -i "%%a" -vcodec copy -acodec libfaac "Z:\newfiles\%%~na.mp4"
pause
Try this for variable bit rate (VBR):
for %%a in (*.mkv) do C:\ffmpeg\bin\ffmpeg -i "%%~a" -vcodec copy -c:a libfaac -q:a 100 "%%~na.mp4"
pause
Range for -q:a is 10-500 and is similar to using the -q option in standalone faac. 100 is a good value to try.
or try this for average bit rate (ABR):
for %%a in (*.mkv) do C:\ffmpeg\bin\ffmpeg -i "%%~a" -vcodec copy -c:a libfaac -b:a 192k "%%~na.mp4"
pause
FFmpeg and AAC Encoding Guide: libfaac
Note that you will not get as good results as with libfdk_aac.
The license of libfdk_aac is not compatible with the GPL, so the GPL does not permit distribution of binaries containing code licensed under these licenses when GPL-licensed code is also included. Therefore this encoder has been designated as "non-free", and you cannot download a pre-built ffmpeg that supports this. This can be resolved by compiling ffmpeg yourself. You can do this for example with the help of this project.

ffmpeg batch retains old filetype in name

I'm using the following ffmpeg script to convert all .mp4 files in the folder to .webm.
for %%A IN (*.mp4) DO ffmpeg -i "%%A" -c:v libvpx -crf 4 -b:v 3M -c:a libvorbis "%%A.webm"
I am trying to make it go from "xxx.mp4" to "xxx.webm"
However when I run the script the file name goes from "xxx.mp4" to "xxx.mp4.webm"
Any suggestions are highly appreciated.
Use %%~nA.webm to select the name-part of the filename.
see for /? from the prompt or documentation.
To recursively convert in webm all the mp4 video files in nested folders, you can try this command:
find '~/Video/' -iname '*.mp4' -exec bash -c 'D=$(dirname "{}"); B=$(basename "{}"); mkdir "$D/webm/"; ffmpeg -i "{}" -c:v libvpx -crf 4 -b:v 3M -c:a libvorbis "$D/webm/${B%.*}.webm"' \;
It will create a folder named "webm" inside the one with mp4 video files and, inside the "webm" folder, it will save relative webm files, without keeping the old file extension in the name.

Resources