Ffmpeg command for sketching sprites

To output one frame from ffmpeg I can do:

ffmpeg -i input.flv -ss 00:00:14.435 -vframes 1 out.png

      

And output an image every second, I can do:

ffmpeg -i input.flv -vf fps=1 out%d.png

      

Would there be a way to create a thumbnail sprite from this output to help with generating the vtt file for the thumbs at the search position?

+1


source to share


2 answers


I'm not really sure what you need / what sprite means for a vtt file, but there is a nice tool that allows you to combine individual images into a large overview image:

ImageMagick comes with a handy toolmontage

montage - create a composite image by combining several separate images. The images alternate in a composite image, additionally decorated with a frame, frame, image name, etc.



You can put sketches together on one or more images with the following command:

montage *.png -tile 4x4 overview.png

      

It will automatically generate the number of images needed to give you an overview.

+2


source


Here is an example of creating jpeg files (280x180) from mp4 video using ffmpeg and then assembling those thumbnails into a sprite (png format) using PHP gd2 + to write a VTT file for a video player.

First, create 1 image per second using ffmpeg:

ffmpeg -i sprite/MyVideoFile.mp4 -r 1 -s 280x180 -f image2 sprite/thumbs/thumb-%%d.jpg

      

Then create sprite file + vtt file (PHP example):



$dirToScan      =   'thumbs/';
$filePrefix     =   'thumb-';
$fileSuffix     =   '.jpg';
$thumbWidth     =   280;
$thumbHeight    =   180;
$imageFiles     =   array();
$spriteFile     =   'sprite.png';
$imageLine      =   20;
$vttFile        =   'sprite.vtt';
$dst_x          =   0;
$dst_y          =   0;


# read the directory with thumbnails, file name in array
foreach (glob($dirToScan.$filePrefix.'*'.$fileSuffix) as $filename) {
    array_push($imageFiles,$filename);
}

natsort($imageFiles);
#calculate dimension for the sprite 
        $spriteWidth =  $thumbWidth*$imageLine;
        $spriteHeight   =   $thumbHeight*(floor(count($imageFiles)/$imageLine)+1);

        # create png file for sprite
        $png = imagecreatetruecolor($spriteWidth,$spriteHeight);

        # open vtt file
            $handle =   fopen($vttFile,'wb+');
            fwrite($handle,'WEBVTT'."\n");

        # insert thumbs in sprite and write the vtt file
            foreach($imageFiles AS $file)   {
                $counter        =   str_replace($filePrefix,'',str_replace($fileSuffix,'',str_replace($dirToScan,'',$file)));
                $imageSrc = imagecreatefromjpeg($file);
                imagecopyresized ($png, $imageSrc, $dst_x , $dst_y , 0, 0, $thumbWidth, $thumbHeight, $thumbWidth,$thumbHeight);

                $varTCstart =   gmdate("H:i:s", $counter-1).'.000';
                $varTCend   =   gmdate("H:i:s", $counter).'.000';

                $varSprite  =   $spriteFile.'#xywh='.$dst_x.','.$dst_y.','.$thumbWidth.','.$thumbHeight;

                fwrite($handle,$counter."\n".$varTCstart.' --> '.$varTCend."\n".$varSprite."\n\n");

create new line after 20 images
                if ($counter % $imageLine == 0) {
                    $dst_x=0;
                    $dst_y+=$thumbHeight;
                }
                else    {
                    $dst_x+=$thumbWidth;
                }

            }
            imagepng($png,$spriteFile);
            fclose($handle);

      

VTT file looks like this:

WEBVTT
1
00:00:00.000 --> 00:00:01.000
sprite.png#xywh=0,0,280,180

2
00:00:01.000 --> 00:00:02.000
sprite.png#xywh=280,0,280,180

3
00:00:02.000 --> 00:00:03.000
sprite.png#xywh=560,0,280,180
...

      

+1


source







All Articles