How to convert all files in a specific directory using batch script - batch-file

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.

Related

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

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?

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.

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.

Pandoc Windows batch file builds wrong path openBinary file does not exist

THE ISSUE
This may just stem from a lack of deep understanding of Windows batch file coding.
I am trying to write a simple one-line batch file that will process every file in a directory using pandoc to convert all doc or docx (MS Word) files to markdown (.md) files. When I run my batch file I get the following error:
pandoc: C:_ALL\_ALL\accomp\testing-accomp-2017.05\20170505.md: openBinaryFile: does not exist (No such file or directory)
I get one of these errors for each file in the directory (around 25, or so).
The directory I'm running my command in looks like this:
C:\_ALL\!accomp\testing-accomp-2017.05
As you can see, for some reason the _ALL is appearing twice. The path it is showing me isn't right for some reason and I'm not sure if it is a pandoc issue or a CMD batch file programming issue.
MY CODE
Here is the code for my batch file:
#echo OFF
:: [Not sure what this does, but have read that it is necessary]
setlocal enabledelayedexpansion
:: MAIN
FOR /r "." %%i IN (*.doc *.docx) DO pandoc -f rst -t markdown "%%~fi" -o "%%~dpni.md"
:: End with a pause so user can copy any text from screen.
ECHO. Done. Press any key to terminate program
PAUSE>NUL
Now, I'm not certain what all these lines of code do, and they may be entirely unnecessary for all I know. However, the main and most important code here is the one that starts with For ..., which is inspired by this Stack Overflow post:
Batch processing Pandoc conversions in Windows
WHAT I'VE TRIED ALREADY
Basically there are about four variations of the same answer in the above linked post and I've tried each of those variations.
The error is caused by delayed expansion and the exclamation mark ! in directory name !accomp.
The command line to execute by FOR expands during execution with file 20170505.doc to:
pandoc -f rst -t markdown "C:\_ALL\!accomp\testing-accomp-2017.05\20170505.doc" -o "C:\_ALL\!accomp\testing-accomp-2017.05\20170505.md"
This command line is parsed by Windows command processor a second time before execution because of enabled delayed environment variable expansion, searching for !variable! reference and replacing them with value of referenced variables.
The string !accomp\testing-accomp-2017.05\20170505.doc" -o "C:\_ALL\! is completely misinterpreted here because of the exclamation marks. So finally executed is:
pandoc -f rst -t markdown "C:\_ALL\\_ALL\accomp\testing-accomp-2017.05\20170505.md"
And the file C:\_ALL\\_ALL\accomp\testing-accomp-2017.05\20170505.md does not exist.
The solution is removing setlocal enabledelayedexpansion as not needed here because of no environment variable used on FOR command line.
#ECHO OFF
FOR /r "." %%i IN (*.doc *.docx) DO pandoc.exe -f rst -t markdown "%%i" -o "%%~dpni.md"
:: End with a pause so user can copy any text from screen.
ECHO Done. Press any key to terminate program ...
PAUSE>NUL
The loop variable i holds here already the full qualified file name. Therefore "%%~fi" can be replaced by "%%i".
And it is better to use ECHO/ instead of ECHO. although neither . nor / is needed here. See DosTips forum topic ECHO. FAILS to give text or blank line - Instead use ECHO/ for the reason.

Too much Image Reduction in Batch File using Imagemagick

I use ImageMagick to perform command line operations on images, such as resizing, reducing quality, etc.
However, I've run into a problem with this script:
SETLOCAL ENABLEDELAYEDEXPANSION
#echo off
REM Make the Modified folder, to store resized images.
if not exist %CD%\Mod MKDIR %CD%\Mod
REM loop through all files in working directory with extensions of .bin
for /r %CD% %%G in (*.bin) do (
set FILENAME=%%~nG
echo !FILENAME!.bin conversion beginning...
echo Beginning conversions...
echo Convert to TIF
REM rename the bin file with a .TIF extension.
copy !FILENAME!.bin !FILENAME!.tif
echo resizing image....
REM convert the tif and resize it to 25%. This is where it reduces to almost nothing!
convert !FILENAME!.tif -resize 25% !FILENAME!.tif
echo Renaming file...
REM rename file to say it had been modified
ren !FILENAME!.tif !FILENAME!-mod.tif
echo Copying file...
REM copy the file to the Mod directory.
copy /y !FILENAME!-mod.tif %CD%\Mod
echo Cleaning up...
REM cleanup.
DEL !FILENAME!-mod.tif
echo Moving on to next file...
)
If I run the command that resizes the image from the command line, it works fine, and reduces the image to the estimated size. In the script, it reduces to extraordinarily small dimensions, we're talking about going from around 2500x5000 down to 25x25, or something similar. It's really strange, since I am executing the exact same statement, albeit in a different manner.
I had originally assumed that it was the for loop acting up, but I stepped through it, pausing as I went, and it resized with that one execution, ensuring that it was not the for loop constantly resizing the image by 25% until it was too small to go any further.
Why is it reducing so small, and how can I fix it?
Edit 1: The reason it's converting bin files is because all of the BIN files are actually TIF files that have been renamed with the bin extension for whatever reason. They are, in fact, images.
I would guess you need to escape the % with another % so 25% becomes 25%%

Resources