View Full Version : How to convert a video to images?
egr
14th August 2020, 06:35
Hi, so I did some googling and according to this article: https://www.alexkras.com/convert-video-to-images-via-command-line/
I should be able to do so with this command in FFMPEG:
ffmpeg -i 1.mkv -r 24 a/output_%05d.png
But I get an error instead: [image2 @ 000000000232d300] Could not open file : a/output_H:\FFMPEG\bin\1.bat5d.png
av_interleaved_write_frame(): I/O error
This one: https://gist.github.com/savvot/9e4316dc68f6111f7b1f
My version: ffmpeg -ss 00:14:13.000 -i "1.mkv" -t 00:00:13.000 -q:v 2 -vf select="eq(pict_type\,PICT_TYPE_I)" -vsync 0 frame%03d.jpg
Looks like I would be able to pick any point in time of a video and batch take screenshots from it for a period of time. Though it doesn't seem to have a way to configure the delay after each shot....as it shown in the above one.
Except I get this error: [image2 @ 000000000068d880] Could not open file : frameH:\FFMPEG\bin\1.bat3d.jpg
av_interleaved_write_frame(): I/O error
What am I doing wrong? Probably lots as I'm not really a CLI kind of person and prefer and dumb down GUI one, but there is no such thing, at least according to my google searches...although there are websites that would do this for you but I'm not going to upload a massive file...that's gonna take forever! So chose to do a local method with something I already have installed.
StainlessS
14th August 2020, 12:08
Maybe of some little help.
test.bat
REM test.bat : With location of ffmpeg.exe in environment PATH, and empty IMG directory relative to test.bat file.
REM Gotta have double %% in bat for it to work, or single if direct on command line, also used %%06d (not %%05d) as 100,000 [0->99999] frames max potentially a bit low for long src clip.
REM r 1, 1 frame per second (originally 24)
REM writes png files to IMG directory relative bat file.
ffmpeg -i 1.mkv -r 1 "IMG/output_%%06d.png"
Or to same dir as bat file (using bat file, so using double '%%')
ffmpeg -i 1.mkv -r 1 "output_%%06d.png"
Or to explict path to write at H:\IMG\ (using bat file, so using double '%%')
ffmpeg -i 1.mkv -r 1 "H:\IMG\output_%%06d.png"
or direct in CLI command (not in bat file, so using single '%')
ffmpeg -i 1.mkv -r 1 "H:\IMG\output_%06d.png"
EDIT:
Your "a/output_%05d.png", with single '%', CLI BAT processing, interprets the name of your test.bat as an arg inserted into the output filename
eg (from your error line)
"a/output_H:\FFMPEG\bin\1.bat5d.png"
so your original bat file was "H:\FFMPEG\bin\1.bat". [error was originating in bat processing, not ffmpeg]
Hope that you can use above to figure out how to get 2nd comand working, good luck.
egr
15th August 2020, 08:46
Maybe of some little help.
test.bat
REM test.bat : With location of ffmpeg.exe in environment PATH, and empty IMG directory relative to test.bat file.
REM Gotta have double %% in bat for it to work, or single if direct on command line, also used %%06d (not %%05d) as 100,000 [0->99999] frames max potentially a bit low for long src clip.
REM r 1, 1 frame per second (originally 24)
REM writes png files to IMG directory relative bat file.
ffmpeg -i 1.mkv -r 1 "IMG/output_%%06d.png"
Or to same dir as bat file (using bat file, so using double '%%')
ffmpeg -i 1.mkv -r 1 "output_%%06d.png"
Or to explict path to write at H:\IMG\ (using bat file, so using double '%%')
ffmpeg -i 1.mkv -r 1 "H:\IMG\output_%%06d.png"
or direct in CLI command (not in bat file, so using single '%')
ffmpeg -i 1.mkv -r 1 "H:\IMG\output_%06d.png"
EDIT:
Your "a/output_%05d.png", with single '%', CLI BAT processing, interprets the name of your test.bat as an arg inserted into the output filename
eg (from your error line)
"a/output_H:\FFMPEG\bin\1.bat5d.png"
so your original bat file was "H:\FFMPEG\bin\1.bat". [error was originating in bat processing, not ffmpeg]
Hope that you can use above to figure out how to get 2nd comand working, good luck.
:eek:[GASPS!] It works! I see what I did wrong - the commands are suppose to be directly into cmd window, not in a batch file and saved for a later time; but adding that extra % allows it to be saved in a batch file and works from there anytime, thanks for the little help!:o
New line which combines both aspects:
ffmpeg -ss 00:14:04.000 -i "1.mkv" -t 00:00:12.000 -r 23.97602397602398 a/output_%%04d.png
That should make it so that ffmpeg jumps to 14:04, takes lossless image shots and with a delay equivalent to the frame rate of the video and for 12 seconds flat. Saves all png images in a folder called "a" using a 4 decimal naming scheme. The -i I think is tells it to only take keyframe shots and not any others. :D
StainlessS
15th August 2020, 11:50
using a 4 decimal naming scheme.
You will have to keep changing the "%04d" (or "%04d") when your requirements output more than 10,000 files,
best if you adopt a standard max number of frames, 6 digits cope with up to a million frames, and no screwing around when you change requirment. (avisynth standard is 6 digits with good reason).
A longish single movie can easily have hundreds of thousands of frames at standard 24/25FPS.
Also, any script to process output frame names might have to be edited whenever you change 4 decimal naming scheme, so best stick with something that dont need constant editing.
The -i I think is tells it to only take keyframe shots and not any others.
I dont see any "-i I", (just -i "1.mkv" which is input filename.)
I think that this is the keyframe selection : -vf select="eq(pict_type\,PICT_TYPE_I)"
EDIT: Maybe removing "-r 23.97602397602398" will just output all frames using input framerate for 12 seconds, dont know.
egr
25th August 2020, 06:11
You will have to keep changing the "%04d" (or "%04d") when your requirements output more than 10,000 files,
best if you adopt a standard max number of frames, 6 digits cope with up to a million frames, and no screwing around when you change requirment. (avisynth standard is 6 digits with good reason).
A longish single movie can easily have hundreds of thousands of frames at standard 24/25FPS.
Also, any script to process output frame names might have to be edited whenever you change 4 decimal naming scheme, so best stick with something that dont need constant editing.
I dont see any "-i I", (just -i "1.mkv" which is input filename.)
I think that this is the keyframe selection : -vf select="eq(pict_type\,PICT_TYPE_I)"
EDIT: Maybe removing "-r 23.97602397602398" will just output all frames using input framerate for 12 seconds, dont know.
Oh I see, fair enough I guess I might as well stick with a higher digit output.
Oh I guess I was wrong then if your bit is to select keyframes....
You're right, leaving the -r command and value outputs frames using input framerate source....
Hows it looking now:
ffmpeg -ss 00:00:00.000 -i "grab.mp4" -vf select="eq(pict_type\,PICT_TYPE_I)" -t 00:00:08.000 e/output_%%06d.png
Should take the first 8 seconds of the video file "grab.mp4" and export all keyframes at source framerate, whatever that is. Perfect piece of code when you want a cheap way to turn a video file into images to do some GIF or APNG files! Or for examining the video file more closesly for whatever details you need I guess.
StainlessS
25th August 2020, 14:59
I dont see anything obviously wrong, but then I aint no expert, just a novice.
egr
26th August 2020, 15:16
I dont see anything obviously wrong, but then I aint no expert, just a novice.
Oh well thanks for helping then!
Cary Knoop
26th August 2020, 15:22
Any particular reason you only want key frames?
egr
26th August 2020, 15:36
Any particular reason you only want key frames?
Wouldnt they be cleaner to work with than transitional frames?
Cary Knoop
26th August 2020, 15:55
Wouldnt they be cleaner to work with than transitional frames?
What is the objective?
If you want all extract all the frames for a given time period it is obviously not enough to only select keyframes.
Basically everything is compressed in a video including keyframes. The idea that I frames are great and P and B frames are bad is an unrealistic simplification.
egr
10th September 2020, 17:10
What is the objective?
If you want all extract all the frames for a given time period it is obviously not enough to only select keyframes.
Basically everything is compressed in a video including keyframes. The idea that I frames are great and P and B frames are bad is an unrealistic simplification.
Oh, I thought it was so. So when would only picking out keyframes, make sense then?
butterw2
10th September 2020, 20:29
Keyframes
Depending on encoding parameters, Keyframes may be more meaningful than other frames (ex: inserted on scene changes in movie encodes, they also allow lossless cutting with h264/hevc). There also less keyframes than regular frames.
Ex 100min movie:
- 100*60*24=144000 frames
- 1000 keyframes if average duration is 6s
Keyframes are also used for seeking in video players such as mcp-hc and video editors such as avidemux, vdub2, so they are good to use for screenshots.
Keyframes usually give the best result, when choosing a thumbnail for compressed video.
egr
16th September 2020, 16:40
Oh so basically only good at taking clean shots, seeking and cutting purposes which is what this does.... I'm confused as to why Cary would say its not enough then...perhaps I'm not understanding the concept?
butterw2
20th September 2020, 12:02
As was pointed out, all frames can be used, it just depends what your goal is.
If you don't need all frames: output only 1 frame per second for exemple, or keyframes only.
It's worth noting that all frames of a video could be keyframes in some cases (depends on encoding parameters).
vBulletin® v3.8.11, Copyright ©2000-2026, vBulletin Solutions Inc.