Search code examples
batch-fileffmpegcygwin

Loop through filenames in a folder in Windows and send each name to ffmpeg command


In windows I want to create a batch file which given a specific directory will loop through all the names of the files and pass each of them to a command, ffmpeg in my case. I found two or three examples of ffmpeg being used to loop through a folder, but they were all in Linux. For the time being I use a python script to do so:

import os
import subprocess

sourcedir = "G:\Animation\Anime\OnePiece\Episodes\Main"
outputdir = "G:\Animation\Anime\OnePiece\Episodes\Converted"

for file in os.listdir(sourcedir):
    name = file[:file.rfind(".")]
    subprocess.call("ffmpeg -i " + sourcedir + "\\" + name + ".mkv -s 640x480 -map 0 -vcodec libx265 "+ outputdir + "\\" + name + ".mkv")

Even if running it directly through a Windows batch file instead of via Python decreases the time needed to encode a video by 10 seconds, it will literally save me hours.

And just a thought, will it be any better using the bash commands on Cygwin?


Solution

  • This should get you close. I am not really understanding the syntax of ffmpeg.

     @echo off
     set "sourcedir=G:\Animation\Anime\OnePiece\Episodes\Main"
     set "outputdir=G:\Animation\Anime\OnePiece\Episodes\Converted"
    
     PUSHD "%sourcedir%"
    
     for %%F in (*.mkv) DO ffmpeg -i "%%F" -s 640x480 -map 0 -vcodec libx265 "%outputdir%\%%F"
    
     POPD