ffmpeg: Output to the same folder as source with recursive input? - batch-file

I have a lot of gifs I want converted to webms in many sub directories, and I have this script which will do it, but it will output to the directory where the script is located:
for /r %%a in ("*.gif") do ffmpeg -i "%%a" -c:v libvpx -crf 12 -b:v 4000k "%%~na.webm"
pause
I've tried a bunch of things, but I can't figure out how to get the output to land in the same sub directory as the input file so I can maintain the folder structure.
Edit: it's a Windows batch file I'm using.

You are missing two command modifiers for your output filename.
"%%~dpna.webm"

Related

Troubleshooting ffmpeg batch-file. mkv to mp4 conversion with subtitle hardcode

I am trying to batch convert a large number of .mkv video files to .mp4 while hard coding the subtitles included in the .mkv files. I would like to retain as much quality as possible.
I am not an adapt at using ffmpeg or writing batch files, so I have been searching online for batch files which will do what I need. I found some which successfully convert without the subtitles, and I found one that included the command for hard coding the subtitles, but that one also had excessive commands that I didn't want to delve into, so I am attempting to combined the relevant parts of the two scripts. That is where the problem is obviously coming from.
for %%a in (*.mkv) do ffmpeg -i "%%~a" -vf subtitles=%%~na.mkv copy -c:a aac -q:a 100 "%%~na.mp4"
pause
It is worth noting that the script was originally:
for %%a in (*.mkv) do ffmpeg -i "%%~a" -vcodec copy -c:a aac -q:a 100 "%%~na.mp4"
pause
But I changed it as best I could to include a section of script I found which should apparently hard code the subtitles. My lack of knowledge about how this scripting language actually functions probably just made me insert the command in the wrong place or in the wrong way. I hope that background information makes this problem easier to solve.
Expected results: convert each .mkv file in the current folder and leave the .mp4 file in the same folder.
Actual results:
[NULL # 0000026a399f1440] Unable to find a suitable output format for 'copy': Invalid argument.
Not a batch file user, but I can comment on the ffmpeg command. Use:
for %%a in (*.mkv) do ffmpeg -i "%%~a" -vf subtitles=%%~na.mkv "%%~na.mp4"
pause
You can't use -vcodec copy/-c:v copy with -vf because filtering requires encoding.
If the input audio is already AAC then add -c:a copy to stream copy (re-mux) the audio instead of re-encoding it.
This will rely on the default stream selection behavior. See the -map option if you want to include/exclude certain streams such as if your input has multiple video and/or audio streams.
I figured this out, thanks to comments from people here which helped me search out relevant info.
One of the main, and most embarrassing problems is that the original batch file was in Linux format and I am running on Windows.
This is the working batch file that I pieced together by editing someone else's batch file. I will leave a link to the original batch file as well.
for %%A IN (*.mkv) DO ffmpeg -i "%%A" -vf subtitles="%%A" "%%A.mp4"
pause
This is where I got the original code from: https://superuser.com/questions/470500/batch-convert-avi-files-using-ffmpeg#470510
And this is where I got information about hard coding the subtitles: https://superuser.com/questions/932730/ffmpeg-mkv-to-mp4-conversion-loses-subtitles
Thanks again :)

FFMPEG batch file to re-encode movies that don't contain string "h265" in file name?

I have a batch file that uses FFMPEG to re-encode movies to h265 to reduce file size. I have a few videos that I previously re-encoded over the past couple of years. I want to skip those and at the moment I just manually move them from subfolders before running my script, but this is getting tedious. I'd like to add a command to my batch file that skips those files automatically. All files that have been re-encoded in the past have h265 appended before the file extension. My batch file looks like this, but it isn't skipping the files with h265 in them:
for %%a in ("*.*") do findstr /v /c:"h265" "%%a" >nul && \
ffmpeg -i "%%a" -c:v libx265 -preset ultrafast \
-x265-params [parameters] -c:a copy "%%~na.h265.mp4"
pause
What do I need to change to get this to work?

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.

How to convert audio in each directory by FFmpeg and batch script.

After a hard study, I have created a batch file:
for /R D:\storytelling\MusicFiles %%a in (*.3gp) do ffmpeg -i %%a -y %%~na.mp3
However, it can't reach my goal since the scenario is a little different here:
Here is my directory structure:
I have several directories under a certain path:D:\storytelling\MusicFiles\ and new directories could be created by another application. And I put ffmpeg.exe file in this path: D:\storytelling\MusicFiles\.
Also, In each directory, I have hundreds of .3gp files, and my target is to convert them to .mp3 in the directory where they used to stay at.
But this script
for /R D:\storytelling\MusicFiles %%a in (*.3gp) do ffmpeg -i %%a -y %%~na.mp3
would convert every .3gp file in each directory to the path: D:\storytelling\MusicFiles\
It leads to:
But I want the publicUser_XXX.mp3 files are still in the directory publicUser and after conversion all the files remain in their original directory. The only change is that I got new a media copy with different media format like:
Please help and give me some advise.
Try this:
#echo off
for /R D:\storytelling\MusicFiles %%a in (*.3gp) do ffmpeg -i "%%a" -y "%%~dpna.mp3"

How do i create a batch file that iterates through the files in a folder and executes another .bat on them?

i have a folder full of .avi files and i want to use ffmpeg to convert them to .mp4 files. I followed an online tutorial to create a batch file that does just this, here is the code:
"C:\ffmpeg\ffmpeg.exe" -y -i %1 -sameq -ar 22050 -vcodec libx264 "C:\videos\Series 1\S01E01.mp4"
This works fine if i just drag and drop the .avi files onto the createmp4.bat file containing the above code. However i want to be a little bit smarter about this and use another batch file that will iterate through the .avi files in the folder and run createmp4.bat on all of them and copy them to the C:\videos\Series 1\ directory.
I would also like to change the name of the files if possible to S01E01.mp4, S01E02.mp4 and so on if possible.
Any help on this would be greatly appreciated. Oh and just in case you hadn't guessed i am fairly clueless about writing batch files!!
Thanks
try this (example):
#echo off & setlocal
pushd "X:\My AVI folder"
for %%i in (*.avi) do "C:\ffmpeg\ffmpeg.exe" -y -i "%%~fi" -sameq -ar 22050 -vcodec libx264 "C:\videos\Series 1\%%~ni.mp4"
popd

Resources