for-loopbatch-fileffmpeg

How to execute this CMD Script in a Batch File?


I am trying to make a batch file that when executes makes a list named VideosToJoin.txt of the files with the extension .mp4, .mts and .avi in the folder and the after that proceed with the concat of those same files creating a final one named FinalVideo.mp4 I need some help here, because i want to automatize a specific process...

(for %i in (*.mp4,*.mts,*.avi) do @echo file '%i') > VideosToJoin.txt   
  
ffmpeg -f concat -i VideosToJoin.txt -c copy FinalVideo.mp4

The expected result is to have the final video by executing just one file, that's all i want, please help.


Solution

  • A CMD Script requires that you double up the percent signs on a FOR loop.

    Further you will need to specify what directory you want the script to check, because otherwise it will only check the CURRENT Directory, which is usually USER's Home Directory but could actually be any arbitrary directory depending on other factors.

    I surmise you want to output the file to the same folder that the script will check so that much is easy, and we'll store the temporary videos file to the directory the script is in using the %~dp0 variable. %0 refers to the script itself, and ~dp tells it to return the drive and path to the script.

    Also make sue that FFMpeg is in your path, or specify the full path to it in your script.

    SET "_SrcPath=C:\Some\Folder\Path"
    DEL /F /Q "%~dp0VideosToJoin.txt" 2>NUL
    
    For %%i in (
      "%_SrcPath%\*.mp4"
      "%_SrcPath%\*.mts"
      "%_SrcPath%\*.avi"
    ) do (
      echo file '%%i'
    )>>"%~dp0VideosToJoin.txt"
    
    ffmpeg -f concat -i "%~dp0VideosToJoin.txt" -c copy "%_SrcPath%\FinalVideo.mp4"