batch-filevideoffmpegsubtitleforfiles

Ffmpeg it's not setting the correct variable names and hence throwing Unable to choose format


I'm trying to automate the subtitle addition to a folder with some videos so I wrote the following script:

forfiles /p "C:\Users\titos\Resilio_Sync\4K_Video_Downloader\videoprocess" /m *.mp4 /C "cmd /c set filename=@fname & ffmpeg -i @file -vf subtitles="C:\Users\titos\Resilio_Sync\4K_Video_Downloader\videoprocess\@fname.srt" -f mp4 "C:\Users\titos\Resilio_Sync\4K_Video_Downloader\donesubs\@fname_srt.mp4""

However, the script is throwing me the following error for each video:

[AVFormatContext @ 0000020c20220680] Unable to choose an output format for 'Der'; use a standard extension for the filename or specify the format manually.
[out#0 @ 0000020c20220580] Error initializing the muxer for Der: Invalid argument
Error opening output file Der.
Error opening output files: Invalid argument

In this case, "Der" is the second word of the video's title (And it does the same with each video where it throws the error with the second word). So I think it might be due to the videos containing spaces and ffmpeg not parsing it correctly? But why is the second word that it gets and not the first one in that case? And more importantly, what could I do to fix this issue?

I've honestly found some other threads that discuss similar issues, but quite frankly I'm still too newbie to understand any of the answers in order to adapt it to my script lol. I appreciate your help, thanks a lot in advance :)


Solution

  • I set up a directory with some dummy files matching the problem name format and simply echoed the resultant ffmpeg line. The result was of the form

    set filename="1 der 1" _and_ ffmpeg -i "1 der 1.mp4" -vf 
    subtitles=subtitledir\"1 der 1".srt -f mp4 donesubsdir\"1 der 1"_srt.mp4
    

    (OK - I've broken the line for ease of viewing and substituted subtitledir and donesubsdir for the directorynames.)

    Observing that the quoted filename appears within a string and theorising that ffmpeg may have difficulty with that structure, I suggest

    SET "vpath=U:\Users\titos\Resilio_Sync\4K_Video_Downloader\videoprocess"
    FOR /f "delims=" %%e IN ('dir /b /a-d "%vpath%\*.mp4" 2^>nul') DO ECHO ffmpeg -i "%vpath%\%%~ne" -vf subtitles="subtitledir\%%~ne.srt" -f mp4 "donesubsdir\%%~ne_srt.mp4"
    

    Where you need to follow the bouncing ball for subtitledir and donesubsdir.

    My results were:

    ffmpeg -i "U:\Users\titos\Resilio_Sync\4K_Video_Downloader\videoprocess\1 der 1" -vf 
    subtitles="subtitledir\1 der 1.srt" -f mp4 "donesubsdir\1 der 1_srt.mp4"
    

    (OK - I've broken the line for ease of viewing and substituted subtitledir and donesubsdir for the directorynames.)

    Theory:

    The command dir /b /a-d "%vpath%\*.mp4" 2^>nul is executed and produces lines of the filename only (/b) with no directorynames (/a-d) which are applied to %%e."delims=" tells for that no tokenising is required, so the entire name appears in %%e, The 2^>nul suppresses error messages - the ^ tells cmd that the > is part of the dir command, not the for.

    (there are hundreds, if not thousands, of similar applications posted on SO)

    See for /? or thousands of examples on SO for the use of metavariable-modifiers.

    Then %%~ne is the name-part only of the filename in %%e.