13

I can use the following command to extract images from a video using ffmpeg, saving them to the filesystem:

... | ffmpeg -i - -f image2 'img-%03d.png'

I would like to pipe these images to another application, rather than save them to the filesystem - how they are separated in the pipe is of little importance, since it'll be a NodeJS script that I control myself.

The following does not work:

... | ffmpeg -i - -f image2 pipe: | ...

Erroring with

[image2 @ 0xe1cfc0] Could not get frame filename number 2 from pattern 'pipe:' (either set updatefirst or use a pattern like %03d within the filename pattern)
av_interleaved_write_frame(): Invalid argument

Is there any way to pipe out images extracted from a video using ffmpeg, without using the filesystem?

4
  • Try -f png - | ...
    – Gyan
    Mar 2, 2016 at 12:44
  • @Mulvya This results in erroring with [NULL @ 0x10e2860] Requested output format 'png' is not a suitable output format pipe:: Invalid argument.
    – Birjolaxew
    Mar 2, 2016 at 14:28
  • 2
    Try -f image2pipe -vcodec png - | .... PNG may not work, if so output will be MJPEG after removing vcodec.
    – Gyan
    Mar 2, 2016 at 14:41
  • @Mulvya That did indeed work. Thank you very much :)
    – Birjolaxew
    Mar 2, 2016 at 15:23

1 Answer 1

19

yes there is!

add the argument -f image2pipe, and specify the output filename as a single hypen (-, in this context it means stdout), and specify the input filename also as a single hypen (-i -, in this context it means stdin) and explicitly specify the output format (eg for png: -c:v png), here is a complete example:

ffmpeg -i - -ss 00:00:3 -s 650x390 -vframes 1 -c:v png -f image2pipe -
5
  • Now how do I read it in my program ? Where does one image end and other start ? Does it only support png ?
    – frakod
    Sep 18, 2021 at 13:37
  • @TanishqBanyal your program reads the image from ffmpeg's stdout. this only extracts a single image, so there is no next-image. i think it supports all image formats supported by ffmpeg
    – hanshenrik
    Sep 19, 2021 at 8:01
  • Maybe not in your example, but when you ask ffmpeg for multiple images, it will send multiple images as bytes through stdout pipe. Then you'll have to manually seperate to individual images by either using magic numbers or by calculating size using pixel format and resolution in your program.
    – frakod
    Sep 19, 2021 at 8:20
  • @TanishqBanyal that sounds difficult. if you change format to bmp, byte 3-7 contains a little-endian uint32 size_of_entire_image, so you can use byte 3-7 to check where the images starts and ends. i'm sure you can do something similar with .png as well, but i don't know where the size header is for png.
    – hanshenrik
    Sep 19, 2021 at 18:21
  • @TanishqBanyal byte 3-6* so like const uint32_t size_of_this_image = HETOLE32(*(uint32_t*)&streamBytes[2]); gets you the size of the first image, and so on~ (*that code is not valid for obscure architectures which doesn't allow non-aligned memory access. all normal x86/ARM cpus allow it, but if you want to be truly portable, you should do some memcpy() stuff blah blah)
    – hanshenrik
    Sep 20, 2021 at 7:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .