Search code examples
windowsfor-loopbatch-filereplacecmd

Remove characters from string using cmd.exe batch file and ffmpeg


Here is the code...

@echo off
:: set paths (no quotes)
set ffmpeg=F:\Video\Ffmpeg\ffmpeg.exe
set infolder=F:\Downloads
set outfolder=F:\Video\Finished



@echo.
@echo on
for %%f in (%infolder%\*.mp4) do "%ffmpeg%" -i "%%~dpnf.mp4" -c copy "%outfolder%\%%~nf.mp4"
@echo off
if errorlevel 1 pause
@echo.

This code converts an mpeg-dash file to mp4. An mpeg-dash is a streaming file format.

The input and output file looks like this: "filename - DASH_V.mp4". (no quotes)

I would like the output file to have the " - DASH_V" removed, so it looks like "filename.mp4". (no quotes)

Any help would be appreciated. Could be a quick fix like changing "%%~dpnf~-13.0.mp4" to last command, but I am clueless on syntax as you can see.

Tried a million things and got nowhere. Clueless on syntax.


Solution

  • Here's the code that I tested. I tried to keep it close to the original.

    @ECHO OFF
    SETLOCAL
    rem The following setting for the directories are names
    rem that I use for testing and deliberately include spaces to make sure
    rem that the process works using such names. These will need to be changed to suit your situation.
    
    SET "sourcedir=u:\your files"
    SET "destdir=u:\your results"
    
    @echo off
    :: set paths (no quotes)
    set "ffmpeg=F:\Video\Ffmpeg\ffmpeg.exe"
    set "infolder=F:\Downloads"
    set "outfolder=F:\Video\Finished"
    
    set "ffmpeg=ffmpeg.exe"
    
    
    echo/
    echo off
    for %%E in ("%sourcedir%\*.mp4") do (
     ECHO "%%~dpnE.mp4"
     "%ffmpeg%" -i "%%~dpnE.mp4" -c copy "%destdir%\%%~nE.mp4" >NUL 2>NUL
     IF ERRORLEVEL 1 CALL ECHO failed ON "%%~dpnE.mp4" with ERRORLEVEL %%errorlevel%%&GOTO fail
    )
    @echo off
    if errorlevel 1 PAUSE
    :fail
    echo/
    GOTO :EOF
    

    First, I set the value of ffmpeg to a value that suits my system. This obviously needs to be executed after ffmpeg is set by the original code.

    Next, I use echo/ to echo a blank line. This appears to overcome the problems with echo. and echo(

    I removed the unnecessary @s - only needed where echo is on. The echo off before the for statement is a placeholder which was originally ON for debugging, but I was not interested in listing the commands executed.

    I changed the metavariable to %%E because I prefer to avoid ADFNPSTXZ (in either case) as metavariables (loop-control variables). ADFNPSTXZ are also metavariable-modifiers which can lead to difficult-to-find bugs (See for/f from the prompt for documentation)

    The filename is now quoted as the path contains spaces.

    Then I echoed the filename being processed.

    Then execute ffmpeg, but suppress the output and error-output (easier to find my messages that way)

    Then - the test of errorlevel should be executed immediately after the ffmpeg as errorlevel will be set to the value the last time ffmpeg was executed, so a sequence of ...fail...pass... would leave errorlevel set at 0, not 1 as expected by the original code.

    I found that the result generated in the destination directory was filename.mp4 as expected, not filename - DASH_V.mp4 as appeared to be the case on first reading.

    If it is in fact the case that the conversion of "dash videos" whatever.mp4 is creating filename - DASH_V.mp4 as claimed, then

    ren "%outfolder%\%%~nf - DASH_V.mp4%" "%~nf.mp4"
    

    directly after the if errorlevel 1 directly following the ffmpeg line should rename as desired.

    I have no video files that create a file with the claimed name, so unfortunately I can't test that.