I've done this batch file that should convert all mp3 files in given folder but, I dont know why, it doesn't work...
Here is the code:
#echo off
title Converting...
set fold=C:\Users\Username\Music\Music to convert\
set ext=*.mp3
set dest=C:\Users\Username\Music\Converted Music\
for %%f in (%fold%%ext%) do ffmpeg -i %%f -b 192k %dest%%%f
pause>nul
exit
I think it's because of the spaces in the files names and in the folder name but i don't actually know... can someone verify and find out the right code?
Thanks...
Easy.
for %%f in ("%fold%%ext%") do ffmpeg -i "%%f" -b 192k "%dest%%%~nxf"
(modified version)
Here's my test batch
#echo OFF
SETLOCAL
REM title Converting...
set fold=U:\Users\Username\Music\Music to convert\
set ext=*.mp3
set dest=U:\Users\Username\Music\Converted Music\
:: This makes the directories, then creates two dummy files
:: only used for testing.
MD "%fold%"
MD "%dest%"
dir>"%fold%file1.mp3"
dir>"%fold%file two.mp3"
for %%f in ("%fold%%ext%") do ECHO ffmpeg -i "%%f" -b 192k "%dest%%%~nxf"
The ECHO in the last line is to show what would be executed. After verification, remove the ECHO keyword to activate the FFMPEG command.
%%~nxf is the name+extension of the file %%f
results:
ffmpeg -i "U:\Users\Username\Music\Music to convert\file1.mp3" -b 192k "U:\Users\Username\Music\Converted Music\file1.mp3"
ffmpeg -i "U:\Users\Username\Music\Music to convert\file two.mp3" -b 192k "U:\Users\Username\Music\Converted Music\file two.mp3"
Hmmm...perhaps you need to replace USERNAME when you're setting FOLD and DEST with %username%
revised to add processing for multiple filetypes
#echo OFF
SETLOCAL
REM title Converting...
set fold=U:\Users\Username\Music\Music to convert\
set ext=*.mp3 *.wma *.m4a
set dest=U:\Users\Username\Music\Converted Music\
:: This makes the directories, then creates dummy files
MD "%fold%"
MD "%fold%subdir"
MD "%dest%"
dir>"%fold%file1.mp3"
dir>"%fold%file two.mp3"
dir>"%fold%file3.wma"
dir>"%fold%subdir\file four.wma"
for /r "%fold%" %%f in (%ext%) do ECHO ffmpeg -i "%%~ff" -b 192k "%dest%%%~nxf"
Noting that I haven't changed the FFMPEG command - you are aware of the parameters you require. Command echoed as usual.
If you want to use a constant output extension, say .MP3, change "%dest%%%~nxf" to "%dest%%%~nf.MP3"
Revised batch output:
ffmpeg -i "U:\Users\Username\Music\Music to convert\file1.mp3" -b 192k "U:\Users\Username\Music\Converted Music\file1.mp3"
ffmpeg -i "U:\Users\Username\Music\Music to convert\file two.mp3" -b 192k "U:\Users\Username\Music\Converted Music\file two.mp3"
ffmpeg -i "U:\Users\Username\Music\Music to convert\file3.wma" -b 192k "U:\Users\Username\Music\Converted Music\file3.wma"
ffmpeg -i "U:\Users\Username\Music\Music to convert\subdir\file four.wma" -b 192k "U:\Users\Username\Music\Converted Music\file four.wma"
Related
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"
)
I have started a huge video project which I am trying to automate. So far, I made good progress, but now I am stuck. I have searched in Google and here, in Stack OverFlow for answers, but can’t find the solution to my problem.
For completion I will explain my full project.
First step of my project is to convert all of my files to *.mp4 and in the same run burn my logo and website in the video. I do this with this code:
for %%a in ("*.*") do ffmpeg -i "%%a" -i "C:\Users\PC03\Desktop\Video test\Overlay.png" -filter_complex "[0:v][1:v] overlay" -c:v libx264 -preset medium -crf 23 -c:a aac -b:a 128k "newfiles\%%~na.mp4"
pause
After that, I will place all the video files in their relevant subfolder. This way I have almost a hundred subfolders, with 2 or 3 files in them. Per submap I want a video with an intro (not in the submap) the video’s in the submap, and an outro (not in the submap).
I can do the above with this code:
ffmpeg -f concat -i intro.mp4 -i mylist.txt -outro.png -c copy total.mp4
To automate everything, I have this code to make a mylist.txt file per subfolder:
>output.txt (
echo C:\Users\PC03\Desktop\Video test\vast\intro.mp4
(for %%i in (*.mp4) do #echo file '%%i')
echo C:\Users\PC03\Desktop\Video test\vast\outro.mp4
)
Now the only thing which I have to do is automate the following code:
ffmpeg -f concat -i intro.mp4 -i mylist.txt -outro.png -c copy total.mp4
to go through all the subfolders.
I have made this code:
FOR /R "C:\Users\PC03\Desktop\Video test" %%F IN (.) DO (
> %%F\output.txt (
echo "C:\Users\PC03\Desktop\Video test\vast\intro.mp4"
(for %%i in (*.mp4) do (
#echo file '%%i'
))
echo "C:\Users\PC03\Desktop\Video test\vast\outro.mp4"
)
)
This codes provides me with a mylist.txt file in every subfolder, but the content is not what I want. It takes the content of the parentmap instead of the subfolders. I have a strong feeling that somewhere in this code I have to make another FOR /R but I can’t make it work.
Does someone have a suggestion on how to do this?
The FOR /R will iterate the folders but you try to iterate the *.mp4 files without referring to the currently processed (sub)folder.
#Echo off
Set "Base=C:\Users\PC03\Desktop\Video test"
Set "Ext=*.mp4"
FOR /R "%Base%" %%F IN (.) DO (
PushD "%%F"
if exist "%Ext%" (
> "%%~fF\output.txt" (
echo "%Base%\vast\intro.mp4"
For %%I in (%Ext%) do #echo file '%%~fI'
echo "%Base%\vast\outro.mp4"
)
Echo created "%%~fF\output.txt"
)
PopD
)
I have used the program youtube-dl to download a Youtube playlist, i have chosen to download videos and audios separately, i have now a folder full of videos with their corresponding audios that i wish to merge together with ffmpeg.
I need to do this using a batch script but the problem is that youtube-dl added aleatory letters after the original file's title so the videos doesn't have the same name as their corresponding audio, file names looks like this:
First title in the playlist 5J34JG.mp4
First title in the playlist H3826D.webm
Second title in the playlist 3748JD.mp4
Second title in the playlist 6SHJFZ.webm
How to merge these multiple video/audio files using windows batch script and ffmpeg ?
Edit: I forgot to mention that the .webm files are the audio files and that i have multiple files i can't rename them one by one.
#echo off
setlocal
set "outdir=muxed"
if not exist "%outdir%" md "%outdir%" || exit /b 1
:: Get each mp4 file and call the label to mux with webm file.
for %%A in (*.mp4) do call :mux "%%~A"
exit /b
:mux
setlocal
set "videofile=%~1"
set "name=%~n1"
:: Last token of the name is the pattern to remove.
for %%A in (%name:-=- %) do set "pattern=-%%~A"
:: Remove the pattern from the name.
call set "name=%%name:%pattern%=%%"
:: Get the webm filename.
for %%A in ("%name%-*.webm") do set "audiofile=%%~A"
:: Mux if webm file exist and output file does not exist.
if exist "%audiofile%" if not exist "%outdir%\%name%.mp4" (
ffmpeg -i "%videofile%" -i "%audiofile%" -c copy "%outdir%\%name%.mkv"
)
exit /b
The script will 1st make the output directory to save mp4
files so that the output will not become input.
The main for loop will get each mp4 filename. The :mux
label is called with the mp4 filename as an argument.
The variable videofile stores the mp4 filename and
variable name stores just the name without extension.
The last token of name will be the YTID pattern while a
for loop will set the last token to pattern.
The call set will substitute the pattern with "" from the
name. This will give a name that can used with a wildcard to
find the webm filename. A for loop will get the webm
filename.
If the destination does exist and not the output file, then
ffmpeg will mux the 2 files into an output file.
Output file container format is mkv.
Since I was needing to do something similar, I am posting my shorter code to help others out.
This is for in a subfolder
FOR /F "tokens=*" %%f IN ('dir /b /s F2M\*.mp4') DO ffmpeg -i "%%~dpnxf" -i "%%~dpnf.m4a" -c:v copy -c:a aac -map 0:v:0 -map 1:a:0 "F2M\%%~nf-clean.mp4"
This is for if you're doing it in the same directory
for %%f in (*.mkv) do ffmpeg -i "%%~dpnxf" -i "%%~dpnf.m4a" -c:v copy -c:a copy -map 0:0 -map 0:1 -shortest "%%~nf-remux.mp4"
This is if ffmpeg is not in the same folder
set ffmpeg=C:\ffmpeg.exe
for %%a in (*.mkv) do "%ffmpeg%" -i "%%~dpnxf" -i "%%~dpnf.m4a" -c:v copy -c:a copy -map 0:0 -map 1:0 -shortest "%%~nf-remux.mp4"
Just example codes, not specifically formatted for the OP. Use with caution.
I have this small batch script. The purpose is to save small portion of our rtmp live stream
#echo off
echo Live Stream Cropper v0.1
echo **************************
echo video codec : libxh264
echo audio codec : mp3
echo **************************
echo Recording...
echo.
for /f "tokens=2 delims==" %%I in ('wmic os get localdatetime /format:list') do set datetime=%%I
set output=%datetime:~0,8%-%datetime:~8,4%
ffmpeg -i rtmp://my.streaming.path/public/appname -acodec mp3 -vcodec libx264 "X:\PATH\%output%.avi" -nostats -loglevel 0
echo.
echo "Recorded video is saved on X:\PATH\%output%.avi"
pause
The ffmpeg process is working as expected. My problem is after pressing CTRL + C to stop the process of ffmpeg, the echo command to show where the recorded file is saved is not executed.
How can I stop the ffmpeg process and then continue running the remaining of the script?
Thanks
Some programs ignore the /wait option but you can try this. Essentially you will have to spawn ffmpeg into another environment and the main environment will wait for ffmpeg to finish before it continues.
START "FFMPEG" /WAIT ffmpeg -i rtmp://my.streaming.path/public/appname -acodec mp3 -vcodec libx264 "X:\PATH\%output%.avi" -nostats -loglevel 0
Give this a try as well.
cmd /c ffmpeg -i rtmp://my.streaming.path/public/appname -acodec mp3 -vcodec libx264 "X:\PATH\%output%.avi" -nostats -loglevel 0
How can I make a batch file to do this for all the different files in a Directory?
drg2sbg.exe file.drg -o file.sbg
drg2sbg.exe file.drg -t file.txt
drg2sbg.exe file.drg -b file.bmp
Thanks,
for %%a in (*.drg) do (
drg2sbg.exe "%%~a" -o "%%~na.sbg"
drg2sbg.exe "%%~a" -t "%%~na.txt"
drg2sbg.exe "%%~a" -b "%%~na.bmp"
)
where %%a is set to each name of the *.drg file in turn and %%~na extracts the name part.
(as lines in a batch file - reduce %% to % to operate directly from the prompt)