I'm trying to use FFmpeg to extract one frame per second from every file in a directory.
I use this command for individual files, but I can't figure out how to input a directory instead:
ffmpeg -i "C:\input\clip1.mp4" -vf fps=1 -qscale:v 2 "C:\output\clip1\clip1A"%d.jpgIdeally, I want the frames saved to their own folder like below, but I think I can work that part out myself. It's fine if they're all output to the same folder with the same prefix and sequential numbering.
C:\output\clip1\clip1-001.jpg, clip1-002.jpg
C:\output\clip2\clip2-001.jpg, clip2-002.jpg
I've been trying for many hours now and I'm still completely lost. Any help at all would be extremely appreciated.
11 Answer
Done it! This code will output frames to folders of the same name as the video they came from. The frames will be prefixed by the video name and suffixed by sequential numbers starting at 001. So for my files it spits out "/clip1/clip1-001.jpg" etc.
Just in case you're like me and don't have a clue what you're doing, paste the below code in to notepad, save as "extractframesorwhatever.bat", and run from from the directory where your clips are.
for %%F in (*.mp4) do (
If not Exist "%%~nF" MkDir "%%~nF"
ffmpeg -i %%F -r 1 -qscale:v 2 %%~nF\%%~nF-%%3d.jpg
)If you want to output the folders somewhere else, change:
If not Exist "%%~nF" MkDir "%%~nF"To something like this:
If not Exist "C:\wherever\%%~nF" MkDir "C:\wherever\%%~nF"If you do that you'll also need to change the output from (in my example):
%%~nF\%%~nF-%%3d.jpgto
C:\wherever\%%~nF\%%~nF-%%3d.jpgOr if you want all frames in one folder. Replace the first %%~nF from the output with your preferred directory. Using my example, it would go from C:\wherever\%%~nF\%%~nF-%%3d.jpg to C:\wherever\frames\%%~nF-%%3d.jpg. You will also either want to edit the second line to point to the new location and stop it from creating a bunch of unnecessary directories, or take out the second line entirely. If you remove the second line, you'll have to create that "frames" folder yourself first.
EDIT - You might need to remove all spaces from file names before they can be processed. "Bulk rename utility" can save you a lot of time.
1