pythonbatch-filerecursionffmpegvideo-conversion

Recursive ffmpeg script preserving the subfolder structure


I've +300 videos in old format (.VOB) and I want to convert it in mp4. Luckly, after some documentation, I've managed to convert a couple of them, but I need to make it automatic. Don't know how, even which language should I use (python, batch...)

The thing is I want to preserve the subfolder structure. For example, if video 1 is inside folder B which is also inside folder A, the output must be in a folder named "output", and inside the converted video must be inside folder B and folder A

The ffmpeg command, if necessary:

ffmpeg -i input.vob -vf yadif=1 -c:v h264_nvenc output.mp4

Thanks in advance


Solution

  • The task could be done with:

    @echo off
    setlocal EnableExtensions DisableDelayedExpansion
    set "BaseSourceFolder=C:\Temp\Source"
    set "BaseTargetFolder=C:\Temp\Target"
    for /R "%BaseSourceFolder%" %%I in (*.vob) do (
        set "FullSourceFileName=%%I"
        set "TargetFolder=%%~dpI"
        set "TargetFileName=%%~nI"
        setlocal EnableDelayedExpansion
        set "TargetFolder=!TargetFolder:%BaseSourceFolder%=%BaseTargetFolder%!"
        if not exist "!TargetFolder!" md "!TargetFolder!"
        if exist "!TargetFolder!" "ffmpeg.exe" -i "!FullSourceFileName!" -vf yadif=1 -c:v h264_nvenc "!TargetFolder!!TargetFileName!.mp4"
        endlocal
    )
    endlocal
    

    The folder paths of BaseSourceFolder and BaseTargetFolder must be defined in third and fourth line.

    BaseSourceFolder cannot contain character = as otherwise the code would not work.

    The two paths can be identical although in this case it would be more efficient to use a batch file with just one command line.

    The executable file ffmpeg.exe should be specified in batch file with full path for more efficiency as in this case the Windows command processor cmd.exe would not need to search on each VOB file for the executable in current directory and if not found in one directory after the other listed in environment variable PATH until found.

    To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.

    See also this answer for more details about the commands SETLOCAL and ENDLOCAL.