I'm working on a simple batch script that loops over files in a directory and process them with ffmpeg. for readability purposes, I want to indent the ffmpeg output 4 spaces, but I cannot come up with a way that works.
@echo off
for %%a in (%~dp0rawVideo\*.MP4) do (
if exist %~dp0rawAudio\%%~na.wav (
echo Already Processed :
echo %%a
) else (
echo Processing :
echo %%a
echo|set /p leadingSpace="# "
ffmpeg -c:v h264 -threads 24 -i %%a -map_channel 0.1.0 -map_channel -1 -y -v quiet -stats %~dp0rawAudio\%%~na.wav
)
echo(
)
pause
If I just do this i get nothing, because I've read windows strips leading white spaces.
echo|set /p leadingSpace=" "
I tried what I think is the backspace trick as well, but it didn't work. Is there no way of doing this?
1st solution
Here's a way to run your command and prefix each line outputted by a prefix.
Simple example without arguments:
issues:
it redirects standard error to standard output (so error messages are also prefixed) and uses empty delimiter, so
%%a
is the full line.Since you are called each time a line is issued, it is easy to echo it the way you want.
Of course it also works if the command issues just one line.
2nd solution
Since you only have one line, an alternative would be to use a temporary file to get the output of
ffmpeg
, then assign a variable interactively with this file redirected as input. Then echo it as you want:This is a pure Batch file method to create a file with any number of spaces and no line feed at end, so it may be displayed via
type spaces.txt
before aset /P
command in order to show leading spaces:You could pipe the output into any utility that provides search/replace capability. There isn't a native Windows batch command available, but you could use something like JREPL.BAT - a regular expression text processing utility. JREPL.BAT is pure script (hybrid batch/JScript) that runs natively on any Winidows machine form XP onward - no 3rd party exe file is required. Full documentation is available from the command line via
jrepl /?
, orjrepl /??
for paged help.There is a significant delay each time JREPL is called because the CSCRIPT engine must be instantiated, and the JScript must be compiled. Once started, the utility is very fast. But in this case, the fractional second delay for each iteration could be irritating.
Performance can be dramatically improved if you pipe the entire FOR loop output through a single instance of JREPL. The only trick is to structure the search to only match lines that need to be prefixed.
Here I assume that ffmpeg always puts a non-space character in the first position of each line of output, and the ffmpeg output never contains "Process"