how can i convert a video to images using ffmpeg? Example am having a video with total duration 60 seconds. I want images between different set of duration like between 2-6 seconds, then between 15-24 seconds and so on. Is that possible using ffmpeg?
Official ffmpeg documentation on this: Create a thumbnail image every X seconds of the video
Output one image every second:
ffmpeg -i input.mp4 -vf fps=1 out%d.png
Output one image every minute:
ffmpeg -i test.mp4 -vf fps=1/60 thumb%04d.png
Output one image every 10 minutes:
ffmpeg -i test.mp4 -vf fps=1/600 thumb%04d.png
You can use the select filter for a set of custom ranges:
ffmpeg -i in.mp4 -vf select='between(t,2,6)+between(t,15,24)' -vsync 0 out%d.png
Another way is to use ffmpeg library for python, particularly useful if you don't want to add ffmpeg to your pc environment. Start by installing ffmpeg on conda with:conda install ffmpeg
Then you can write a script as bellow:
import ffmpeg
input_file_name = 'input_video.mp4'
(ffmpeg
.input(input_file_name )
.filter('fps', fps=10, round = 'up')
.output("%s-%%04d.jpg"%(input_file_name[:-4]), **{'qscale:v': 3})
.run())
In addition to the select filter in Gyan's answer (which I use with eq rather than between), I came across another filter in the manual: thumbnail
Select the most representative frame in a given sequence of
consecutive frames.
The filter accepts the following options:
n: Set the frames batch size to analyze; in a set of n frames, the filter will pick one of them, and then handle the next batch of n
frames until the end. Default is 100.
Since the filter keeps track of the whole frames sequence, a bigger n
value will result in a higher memory usage, so a high value is not
recommended.
Examples
Extract one picture each 50 frames:
thumbnail=50
Complete example of a thumbnail creation with ffmpeg:
ffmpeg -i in.avi -vf thumbnail,scale=300:200 -frames:v 1 out.png
Related
Lets say video with 1920x1080 3 minutes. i want to split each frame to 4 block. so each block 480x270. then, i want to put block 1 to 4 position. block 4 to 1st position. https://i.ibb.co/QkGpKN6/Naruto-Uzumaki.png
exactly i can extract frame of video to image. then, edit that image. but i got reduce quality, and the size bigger. the disadvantage, it takes twice times.
ffmpeg -r 1 -i input.mp4 -r 1 output_%d.jpg
// convert back image to video after block frame set to specific position in backend.
ffmpeg -r 23.97602397602398 -i output_%d.jpg -c:v h264 -r 23.97602397602398 output.mp4
is there direct way to do this?
FFmpeg's -vf, -af, and -filter_complex options accept a combination of filters as a filtergraph (commas to chain them and semicolons to have multiple chains).
For your purpose, you can try this filter combo first (before crop+overlay):
ffmpeg -i input -vf 'untile=2x2,shuffleframes=0 2 1 3,tile=2x2' output
You probably want different shuffling order. Experiment to figure it out.
Read the doc for their options: https://ffmpeg.org/ffmpeg-filters.html
EDIT
If you want to use crop-overlay instead, try
ffmpeg -i input.mp4 -filter_complex \
"[0]crop=427:240:0:0[v1];\
[0]crop=427:240:427:0[v2];\
[0]crop=427:240:0:240[v3];\
[0]crop=427:240:427:240[v4];\
[0][v1]overlay=x=0:y=0[v5];\
[v5][v2]overlay=x=427:y=0[v6];\
[v6][v3]overlay=x=0:y=240[v7];\
[v7][v8]overlay=x=427:y=240[out]"\
-c:v libx264 -crf 30 output.mp4
Personally, I like the other approach better (a way more compact)
Another option is to use xstack filter instead of overlay filters (one xstack takes care of all the overlay ops).
I wish to extract 10 consecutive frames every 2 seconds.
(this is because I wish to choose "best one" from the "nearby offset").
I know how to extract a frame each x seconds:
ffmpeg -i /tmp/V.MP4 -vf fps=1 %02d.jpg
I know how to extract 10 frames from some starting offset:
ffmpeg -ss 20.0 -i /tmp/V.MP4 -vframes 10 %02d.jpg
I have 2 issues:
How do I find the offset for each output image? I can try and calculate it (using the video fps, which is 29.97 in my case) but it sounds like a bad idea - the data is right there in the video for ffmpeg to grab..
Is there an efficient way to "merge" the two commands into one, therefore getting 10 consecutive frames each x seconds?
Use
ffmpeg -i source -vf select='eq(n,0)+if(mod(trunc(t)+1,2)*(trunc(t)-trunc(prev_t)),st(1,n),lt(n,ld(1)+10))' -vsync 0 -frame_pts 1 %d.jpg
How do I find the offset for each output image?
See what frame_pts values mean, at ffmpeg output images filename with time position
this is because I wish to choose "best one" from the "nearby offset"
the thumbnail filter can sort of do this.
I'm using ffmpeg to take screenshot from online video stream. I want to seek multiple timeline. I've used the following command to capture 1 screenshot by seek command:
ffmpeg -ss 00:02:10 -i "stream-url" -frames:v 1 out1.jpg
How I can take multiple screenshot via multiple seek time. I've searched for the solution but no success.
I've used the following command to take multiple screenshot as follows:
ffmpeg -noaccurate_seek -ss 00:01:10 -i "stream-url" -map 0:v:0 -vframes 1 -f mpeg "thumb/output_01.jpg" -ss 00:02:10 -i "stream-url" -map 1:v:0 -vframes 1 -f mpeg "thumb/output_02.jpg"
Is there any way to generate screenshots from same input via seek command? How to make it more faster? How to skip multiple input(-i param)? I've also tried with other commands but those are more slower. Can anyone help me?
There's no easy way I know to specify a number of arbitrary seek points from which to extract frames (similar question here).
However, seeking is very fast with the way you specified. Instead of constructing a complex command, you could just download the YouTube video using youtube-dl (if you haven't done that already) and generate the commands like this:
ffmpeg -ss 00:01:10 -i input -frames:v 1 out1.jpg
ffmpeg -ss 00:02:05 -i input -frames:v 1 out2.jpg
ffmpeg -ss 00:03:20 -i input -frames:v 1 out3.jpg
Note that exporting JPG might lead to low quality. Using PNG is preferred; you will get lossless frames that you can handle with another program later (e.g. to resize or compress).
If you want to get frames from regular intervals, use the fps filter to drop the framerate:
ffmpeg -i input -filter:v fps=1/60 out%02d.jpg
This will output a frame every minute (1/60 frames per second = 1 frame per minute), with two zero-padded digits as output numbers. You could additionally offset the start by providing a -ss option before the input file.
trying to get my head around ffmpeg to create a slideshow where each image is displayed for ~5 seconds with some audio. created a bat file to run the following so far:
ffmpeg -f image2 -i image-%%03d.jpg -i music.mp3 output.mpg
It gets the images and displayes them all very fast in the first second of the video, it then plays out the rest of the audio while showing the last image.
I want to make the images stay up longer (about 5 seconds), and stop the video after the last frame (not playing the rest of the song), are either of these things possible? i could hack the frame rate thing i guess by having hundreds of the same image in order to keep it up longer, but this is far from ideal!
Thanks
The default encoder for mpg output, mpeg1video, is strict about the allowed frame rates, so an input and an output -r are required:
ffmpeg -r 1/5 -i image-%03d.jpg -i music.mp3 -r 25 -qscale:v 2 -shortest -codec:a copy output.mpg
The input images will have a frame rate of 1 frame every 5 seconds and the output will duplicate frames to reach 25 frames per second.
-f image2 is generally not required.
-qscale:v can control output quality. A sane range is 2-5.
-shortest will make the output duration the same as the shortest input duration.
-codec:a copy copy your MP3 audio instead of re-encoding.
MPEG-1 video has more modern alternatives. See the FFmpeg and x264 Encoding Guide for more info.
Also see:
* FFmpeg FAQ: How do I encode single pictures into movies?
* FFmpeg Wiki: Create a video slideshow from images
You could use the filter fps instead of output framerate
ffmpeg -r 1/5 -i img%03d.png -i musicfile -c:v libx264 -vf fps=25 -pix_fmt yuv420p out.mp4
This however skips the last image for me strangely.
Anyone knows the trick?
And how to install ffmpeg ? yum install mpeg only returns this:
======================================================================================== Matched: mpeg ========================================================================================
libiec61883.i386 : Streaming library for IEEE1394
libiec61883.x86_64 : Streaming library for IEEE1394
qffmpeg-devel.i386 : Development package for qffmpeg
qffmpeg-devel.x86_64 : Development package for qffmpeg
qffmpeg-libs.i386 : Libraries for qffmpeg
qffmpeg-libs.x86_64 : Libraries for qffmpeg
I've cobbled up this command line from various answers that works great for me to get the absolutely first frame out from a video. I use this to save a thumbnail screenshot for the video.
ffmpeg -i inputfile.mkv -vf "select=eq(n\,0)" -q:v 3 output_image.jpg
Explanation:
The select filter -vf "select=eq(n\,0)" is to select only frame #0.
-q:v allows you to set the quality of the output jpeg between 1 and 31. Lower the number, higher the quality. 2 - 5 works good, I use 3.
Note: This will get you an image with the same size as the video. To get a thumbnail, you can use the scale filter to get a thumbnail to fit whatever width you need, like so:
ffmpeg -i inputfile.mkv -vf "select=eq(n\,0)" -vf scale=320:-2 -q:v 3 output_image.jpg
The above command will give you a thumbnail jpeg that will be scaled to match width of 320, and height will be calculated to match the aspect ratio.
It's on the manpage:
* You can extract images from a video, or create a video from many
images:
For extracting images from a video:
ffmpeg -i foo.avi -r 1 -s WxH -f image2 foo-%03d.jpeg
This will extract one video frame per second from the video and will
output them in files named foo-001.jpeg, foo-002.jpeg, etc. Images
will be rescaled to fit the new WxH values.
If you want to extract just a limited number of frames, you can use
the above command in combination with the -vframes or -t option, or in
combination with -ss to start extracting from a certain point in time.
But of course you have to install it first. I'm on Debian and don't use yum.
[update for the other question]
i=1
for avi in *.avi; do
ffmpeg -i $avi -vframes 1 -f image2 /tmp/$i.jpg; i=$((i+1))
done
Tested and works.
[update for yet another question...]
for flv in *.flv; do
ffmpeg -i $flv -vframes 1 -f image2 ${flv%%.flv}.jpg
done
An easy to grok solution that works for me is
ffmpeg -i <input> -vframes 1 <output>.jpeg
Note that I do get an error "[swscaler # 0x111652000] deprecated pixel format used, make sure you did set range correctly" but according to a little reading (see for example https://stackoverflow.com/a/43038480/1241736) that can safely be ignored.
It's works for me
ffmpeg -i sample-mp4-file.mp4 -ss 1 -vframes 1 output.jpg