Saturday, August 31, 2019

Cartoonize Video Using FFMpeg and Imagemagick

The year would be 1985, Raybans and Rubiks cubes were all the rage and a iconic MTV video hit stormed the MTV circuit.
This one-hit-wonder holds a special place in my heart.  The black-n-white sketch effect to this day is pretty spectacular, thought I'd take a crack at cartoonizing a video using FFMpeg and Imagemagick.  I've yet to get an equivalent effect, but it's still a fun exercise.

Let's use the amazing movie remake 'A Man On Fire' as our source video.

 Let's say we have a scene like this.



And we wish to make it look like this.


Doing this for each frame of the video should give us a sketch or cartoon effect, like a comic book effect.  A far cry from 'Take On Me' but perhaps a step in the right path.

While FFMpeg has performed miracles in the past, we'll have to pair it up with ImageMagick to get this effect.  In short, we'll extract the video into a series of png frames, run each frame through ImageMagick to cartoonize the frame, then we'll reassemble the images into a video with FFMpeg once again.

Say we have an input video file: ManOnFire.mpg

Let's grab a clip;
$ ffmpeg -i ManOnFire.mpg -ss 373 -t 3 -strict -2 clip01.mp4

Then let's extract it into a series of frames;

$ ffmpeg -y -i clip01.mp4 -an -vf scale=360x240 clip01.mp4-f%05d.png -strict -2 clip01.mp4-audio.mp3

Then, let's convert each frame into a cartoonized frame; grab a sandwich and a beer, this step is pretty time-intensive.

$ find . -name "clip01.mp4-*.png" -exec convert {} -sketch 0x20+135 {} \;

Re-add the audio:
$ ffmpeg -y -r 30000/1001 -i clip01.mp4-f%05d.png -i clip01.mp4-audio.mp3 -c:a copy cartoon01.mp4


Do this for a series of clips and you've got a cartoon highlight real of an exceptional movie.

Yes, it's true; Cartoon Denzel has as much swagger as real-life Denzel.

No comments:

Post a Comment