ffmpeg .bat script to change frame rate for multiple clips in a folder? - batch-file

How do I write a .bat file in Windows 10 that changes the frame rate for multiple mp4 video clips in a folder? For example; change the frame rate from 50fps to 25fps (without re-encoding or dropping frames, so that footage is essentially slowed down.)
At the moment these are the commands I've tried using in two separate .bat text files. (I'd like to combine them but don't know how yet).
for %%A IN (*.mp4) DO ffmpeg -y -i "%%A" -c copy -f h264 "%%A.h264"
for %%A IN (*.h264) DO ffmpeg -y -r 25 -i "%%A" -c copy
"%%A_25.mp4"
Problem is these commands don't replace the file extension type, they append to the existing one, ie. '.mp4' becomes '.mp4.h264' then '.mp4.h264_25fps.mp4', and I can't get the second one to work for some reason.
Any advice appreciated. How do I replace the existing file extensions for a group of clips and combine commands into a single .bat?

Related

FFMPEG, convert video same as input size with watermark in Batch

I would like to watermark some videos in a directory. I use a png watermark of 3840x2160 in size. The watermark should go in fullsize on the video. Because my videos have different resolutions I use this batch script
set /p add_watermark=Wasserzeichen Addieren? (Standard: ja/nein):
if /i "%add_watermark%" == "" set add_watermark=ja
if /i "%add_watermark%" == "ja" (
for %%f in (%cd%\output\tmp\*.mp4) do (
set "filename=%%~nf"
ffmpeg -i "%%f" -i %cd%\watermark\watermark.png -filter_complex "[0:v][1:v]scale2ref=oh*mdar:ih[v0][v1];[v0][v1]overlay=main_w-overlay_w-0:0" -c:a copy -map 0 -y "%cd%\output\%%~nf.mp4"
)
)
That works well with videos in 4k but if my input is a HD video it will scale the output up to 4k in the size of the watermark.
If I try to add
-c:v copy FFMPEG is giving out the error
Streamcopy requested for output stream fed from a complex filtergraph. Filtering and streamcopy cannot be used together.
My question is, how can I set the output as same size as the input video with the scaled watermark without doing this in two steps.
Because it seems there is no other idea how to archive this in on step without re-encoding i did a batch script that extracts the resolution from the video files and use it to change the link to a watermark.png in different resolutions.
As example:
watermark_1920x1080.png
watermark_2560x1440.png
watermark_3840x2160.png
and so on. This is only a impractical workarround (but well it works) i let this question open.

Using FFMPEG to batch remove audio endings

I have over 1000 audio files, all of which end in a mouse click. I would like to remove the last half second from all of them. The audio files have different length (i.e. 15sec, 5 sec ...) But one thing in common with all of them is the last half second has a mouse click sound. How do I trim in bulk the ending of the mp3 files within a folder using windows 10 command line? I already have FFMPEG downloaded. Thank you!
This is two questions in one:
How to remove the last 0.5 seconds from inputs of arbitrary durations?
How to incorporate this into a Windows batch script?
I'll answer #1 because I'm not a Windows user. The batch scripting will be up to you.
Get duration using ffprobe:
ffprobe -v error -show_entries format=duration -of csv=p=0 input.mp3
Trim using ffmpeg:
ffmpeg -i input.mp3 -t <duration> -c copy output.mp3
Replace <duration> with the output from ffprobe minus 0.5 seconds.
How to incorporate this into a Windows batch script?
This should do:
FOR %%A IN (*.mp3) DO (
FOR /F %%B IN ('ffprobe.exe -v error -show_entries format^=duration -of csv^=p^=0 "%%~A" ^| xidel -s - -e ". - 0.5"') DO (
ffmpeg.exe -i "%%~A" -t %%B -c copy "%%~dpnA_trimmed.mp3"
)
)
First of all, doing floating point calculations in Batch is officially impossible and unofficially really hard to script. That's why I suggest to let Xidel do the math. It's first of all a command line tool to download and extract data from HTML/XML/JSON, but it can do A LOT more!
Loop over all mp3-files in the current directory.
The ffprobe command as suggested by llogan piped to Xidel to subtract the 0.5s. For example, 25.547755 now becomes 25.047755.
Don't forget to escape the necessary characters inside the for-loop! The = and | in this case.
The ffmpeg command as suggested by llogan, which opens "%%~A (the mp3-file), sets the duration to %%B and creates a new mp3-file (<filename>_trimmed.mp3).
This code assumes the mp3-files, ffprobe.exe, xidel.exe and ffmpeg.exe are all in the same directory.

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 :)

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 convert all files in a specific directory using batch script

I have videos in a folder of .h264 format and i want to convert them to mp4. now by default i can use ffmpeg to do this:
Here is the command:
ffmpeg -i youtube.flv -c:v libx264 filename.mp4
But, new h264 files are being added and I want to keep converting the videos so as to use it for my python script.
I know i can use this to initialize the variable
SET /A num=1
But how do i write a batch script to take all the videos from the directory one by one even though there new videos being added?
I wrote this but this isn't working:
#echo off
SET /A num=1
for file in E:\Records\1\*.h264
do
ffmpeg -i "$file" -c:v libx264 E:\Recods\1\converted\%num%.mp4
move E:\Records\1\"$file" E:\Records\1\done\
set /A num=%num%+1
done
PAUSE
I am making a done folder and moving the videos that have been converted there and in converted folder i am putting converted videos.. Now i just have to run a task scheduler each hour so that if there is a new entry it should convert it and move it to appropriate folder.
You can loop over all files (even with applying a filter) in a folder using the for loop in batch like this:
#echo off
setlocal EnableDelayedExpansion
set /a vid=0
cd /d "your_Folder\Goes here"
for %%f in (*.h264) do (
ffmpeg -i "%%~f" -c:v libx264 "!vid!.mp4"
set /a vid=!vid!+1
)
So what happens here?
The first line is used to have the program running in the correct directory.
After that all files ending with .h264 (might be a different ending in your case; just took it from the question) in that directory are processed using your command. For that the filepath is placed in the spot for the input file and the counter together with .mp4 is placed as the outputfile.
The counter is a bit more tricky because of how sets of parenthesis are evaluated in batch, which is as one whole block. As solution for this there are a lot of questions and answers on SO. Look for "Delayed Expansion batch" and you will find something like this answer.
Placing this as a Task-Scheduler-Task running on a regular basis should keep your folder updated. For actually monitoring the folder I found this impressive code that can be used in combination with the Task-Scheduler to run that on startup. If the monitoring triggers, you can execute the batch-file above and it should be run each time a file is added. You can adjust the powershell-file with file-filters as well to make it fit your needs.

Resources