How to stop ffmpeg and continue batch running - batch-file

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

Related

Run through files and applying filter for similar names BATCH and FFMPEG

I want to be able to run a script in a folder that contains different angles of a video for example: 0007A,0007B,0008A,0008B. I was able to create it by writing the group of videos but I have like 300 different pairs of those video files and I would like to automatize it, here the one I created using 4 angles:
set n=%1
set i=
set /p savingFolder=Please specify URL of the folder to save the files to:
:loop
set i=%i%!
SET /p groupOfVideos=Please write the group of videos you want me to process:
ffmpeg -i %groupOfVideos%E.avi -i %groupOfVideos%F.avi -i %groupOfVideos%G.avi -i %groupOfVideos%H.avi -filter_complex "[0:v][1:v]hstack=inputs=2[top]; [2:v][3:v]hstack=inputs=2[bottom]; [top][bottom]vstack=inputs=2[v]" -map "[v]" -c:v libx264 -pix_fmt yuv420p %groupOfVideos%Z.avi
if i == n GOTO end
move /Y %groupOfVideos%Z.avi %savingFolder%
GOTO loop
As You can see, the loop is created to go back to the question and asks for the next group of video to execute the command with ffmpeg. This time I just would like to include this maybe in a for so each time the for runs gets the pair and then execute the ffmpeg command without asking anything. I have two days thinking on this but maybe I'm looking in a different way.
ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
for %%G in (*A.mp4) do (
set /p MP4A='file %%G'
if exist %MP4A:~0,4%B.mp4 (
set /p MP4B=%MP4A:~0,4%B.mp4
)
ffmpeg -i %MP4A% -i %MP4B% -filter_complex hstack=inputs=2 -c:v libx264 -pix_fmt yuv420p %MP4A:~0,4%L.mp4
pause
)

ffmpeg list video devices with batch script to variables

I use the ffmpeg -stats -hide_banner -list_devices true -f dshow -i dummy command to list all video and audio devices, and I need to get all Alternative name From "DirectShow video devices" (just video devices) with batch script.
I'm start with this: ffmpeg output parse in batch script (first answer)
Can someone more experienced to help me with this task? Thanks in advance!
Refer to #Endoro answer here ffmpeg output parse in batch script.
You can tweak it to get only Video Device List
#echo off
Title ffmpeg list video devices with batch script to variables
set "VideoDevice="
#For /f tokens^=1^,2delims^=^" %%a in (
'ffmpeg -stats -hide_banner -list_devices true -f dshow -i dummy 2^>^&1 ^| findstr /c:"Alternative name"'
) do (
if not defined VideoDevice (
set "VideoDevice=%%~b"
)
)
echo Video Device : "%VideoDevice%"
pause

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

Batch convert all files in a directory with spaces in filename

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"

how to strip a series of characters in dos batch file for filename generation

I've made a batch file for drag and drop conversion of video using ffmpeg.
I'm using avisynth script "avs" as input.
The generated video is "my video file.avs.mp4"
How can i make the generated video filename to be "my video file.mp4" ?
here is my batch:
#echo off
title Converting video for mobile phone... by xXx
ffmpeg.exe -y -i %1 -f mp4 -vcodec libxvid -b 150k -r 25 -maxrate 150k -bt 1k -bufsize 4M -acodec libfaac -ac 1 -ab 32 -ar 22050 -vol 20 -aspect 4:3 %1.mp4
title Finished!!! Press any key to close the window
echo.
echo.
echo.
echo "Success... press any key to close the window."
pause > nul
Thanks in advance!
I suppose your param %1 contains "my video file.avs"
Then this should work
ren "%~1.mp4" "%~n1.mp4"

Resources