Yes that would work, though the center convert command will read into memory ALL the images from the input file stream, before writing them output again. That means it will read in that whole video as uncompressed images in memory! That is a lot of memory.
To save memory so that the command (mogrify for example) only handles one image (frame) at a time, you need something to break the input stream into separate images and process each image, one at a time!
Hmmm.. PNM format is simple enough , in a raw ppm just look a lines starting with 'P' and you found the next image (frame). I could do this in shell but only by using very fancy shell programming. It's easier in perl...
Code: Select all
ffmeg -i "$infile" -f image2pipe -vcodec ppm -. | pnmnoraw |
perl -e '
sub do_cvt {
open(CVT, "|-")
or exec("convert - -flip ppm:-");
print CVT $image;
close CVT;
undef $image;
}
while ( <> ) {
do_cvt if /^P/ && $image;
$image .= $_;
}
do_cvt if $image;
' | ffmpeg -y -f image2pipe -vcodec ppm -i -sameq "$outfile"
It works!
The perl script reads STDIN until it sees the start of the next PPM image. It then feeds the full single PPM image it has read so far into the convert command, which outputs a new single PPM to the perl's STDOUT. The script then continues to record the next image, until EOF, at which point it processes the last image it has read in.
That convert command will only be given ONE single image, and that is all. The whole perl-convert script will processes them while the ffmpeg command continues to generate them, without attempting to read ALL the images into memory first.
That means all three parts of the pipeline will be active, allowing you to process VERY VERY large sequences of images, but with each command only dealing with one image at a time.
The perl can be made more general by making it a script that takes a convert command as a argument.
Just replace the "convert ..." line and specifically the -flip part, with what ever you want!
The above can go further and replace the open-exec-close with a PerlMagick to process the read in "$image" (known as a Blob), in Perl itself, without needing to reinitialize ImageMagick for each image.