32mbit blog
· 9 minutes read · Admin

Cleaning up old 80s/90s cartoons

Cleaning up old 80s/90s cartoons

Get Ready to learn FFmpeg, Buddy

This is my general starting point for encoding 80s and 90s cartoons to HEVC to get decent (and usually, improved over the source) quality and small file sizes. Note that I specifically am referring to Western cartoons; a lot of this will apply to anime too but I have found that anime tends to be released in better shape than the stuff that was made mostly as 23 minute long ads to sell us action figures as kids.

ffmpeg \
      -i "$input" \
        -init_hw_device vulkan=vk:0 \
        -filter_hw_device vk \
        -vf "bwdif=mode=send_frame:parity=auto:deint=all,\
hwupload,\
nlmeans_vulkan=s=3:p=3:r=7,\
hwdownload,\
format=yuv420p" \
        -c:v libx265 \
        -crf 22 \
        -preset medium \
        -x265-params "tune=animation:deblock=-1\\,-1" \
        -c:a aac \
        -b:a 128k \
        -c:s copy \
        -map 0:V \
        -map 0:a:0 \
        -map 0:s? \
        -map_metadata 0 \
        -map_chapters 0 \
      -ignore_unknown \
       $output"

If you are encoding from an interlaced source like DVD, you need to deinterlace it. The bwdif filter in my opinion is the best at this generally. The parity and deint options there are the defaults I think, but parity can be set to tff or bff to manually specify top or bottom field first, and deint can be set to all or interlaced frames only. Still, these defaults are good. The bwdif=mode setting can be changed to send_field if you want to double up the framerate. The result is similar to "bob" deinterlacing, but cleaner, which is what you may want for a lot of TV cartoons of the era that were edited on video. Back then, it was common to sometimes fix animation errors in editing by adjusting playback speed or using a frame store to hold on a frame for longer. It was also common to use character generators to insert graphics elements or titles and credits over the edited video. This is not to mention editing cuts that occur between fields; a decimating deinterlacer will just blend those frames together and your encoding algorithm will hate it. Historically this type of video has been thought of as mixed telecine/video content, but you will drive yourself insane trying to detelecine these (and the results won't be fully correct anyway), so truly it's better to just encode them at 60p or at least 30p. In short, nobody should really be trying to IVTC any Western cartoon from the SDTV era from the 80s to the early 2000s. Fortunately, modern encoding like HEVC doesn't really care much about the increased frame rate--a higher frame rate won't make your files significantly larger. Of course, if your source is already progressive, you can just omit the bwdif filter altogether.

The nlmeans parameters can be adjusted up a bit for stuff that's really noisy, but the tradeoff with nlmeans is stronger denoising will start to smear areas of lower contrast and can be seen in things like fades to and from black, or shading on characters and objects beginning to disappear. If you need slightly heavier denoising I have found

nlmeans_vulkan=s=7:p=5:r=15

works pretty well without getting too smeary.

Note that you really need to use a hardware accelerated nlmeans. I've found the vulkan version to be faster than opencl at least on my AMD card and on linux. Other platforms may have other options, but running nlmeans as a regular CPU filter will net you single digit framerates for the conversions more likely than not.

The CRF value for x265 is personal preference, but 22 is almost completely transparent to me. Using a slower preset (ironically, -preset slower is the most effective time/compression tradeoff) will make your files smaller at the same quality if you have the time. Cartoons cleaned up with nlmeans and the x265 animation tune compress really well, expect 200mb for a half hour. I've found most 23 minute cartoons come in around 150mb or even less per episode.

The other options are related to preserving metadata which is handy if you are outputting to MKV if your source is a DVD, or an MKV file with metadata. There is also a neat trick you can do with ffmpeg to encode directly from DVD titles and chapters. The below example is for a DVD ISO but you can also specify a real drive (at least on Linux with libdvdread set up).

ffmpeg -f dvdvideo -title 1 -chapter_start 1 -chapter_end 2 -i /path/to/dvd.iso

Converting PAL to NTSC

A lot of sources I find online, especially the Internet Archive, tend to be in PAL format because for some reason PAL territories are where obscure old cartoons got full DVD releases. The vast majority of these, if you're lucky, can be deinterlaced to frames (bwdif=mode=send_frame) and then slowed down to 24.000 fps to get a perfect "film" cartoon.


ffmpeg \
       -i "$input" \
-init_hw_device vulkan=vk:0 \
-filter_hw_device vk \
-vf "bwdif=mode=send_frame:parity=auto:deint=all,\
hwupload,\
nlmeans_vulkan=s=7:p=5:r=15,\
hwdownload,format=yuv420p,\
crop=iw*0.92:ih*0.92:iw*0.04:ih*0.04,\
setpts=PTS*(25/24)"\
-r 24 \
-af "asetrate=48000*(24/25),\
aresample=48000"\
-c:v libx265 \
-pix_fmt yuv420p10le \
-preset slower -crf 23 \
-x265-params "tune=animation:deblock=-1,-1" -c:a aac -b:a 128k \
"$output"

The filters of interest here are the video filters setpts=PTS*(25/24) and crop, and the audio filter asetrate=<input sample rate>*(24/25). The setpts filter changes the frame rate by extending the length of time between frames. The crop filter is optional, but I have found that PAL versions of cartoons usually have some dead space around them that will eat up your encoding bitrate. My guess here is that PAL versions probably include all of the NTSC safe area and a little more on top of that to make up for PAL's taller vertical resolution. Anyway, the crop filter takes 4% off the corners which solves this problem nicely. The asetrate filter should be set to whatever your current audio sample rate is, times the 24/25 factor (note that this is the other way around from the setpts filter, as a longer PTS equals a slower framerate). This just slows the audio down by setting its sample rate lower, and the following aresample resamples it back to the original output rate. The resulting audio remains nicely in sync with the slowed down video and pitch corrected too.

The other option is that you are NOT lucky and the video was converted to PAL by some other process. I've noticed this is more likely with late 90s/early 2000s stuff were the NTSC to PAL conversion was likely done digitally. Cartoon Network DVDs tend to suffer from this the most. I'm not sure of the exact method but instead of conforming the video to 25fps, the conversion is temporal and causes awful frame ghosting. The lone upside is that typically on these the audio will already be at the correct pitch and the runtime will be about the same as NTSC. These probably aren't worth trying to fix--get another source, or just deal with it. That said, there are some conversions that introduce a duplicate frame every 24 frames and you might be able to do something with the shuffleframes filter to drop those duplicate frames. (Though, this filter is more useful to restore PAL material that was badly converted to NTSC, but that is a different article.)

What now?

Go watch some of those old Biker Mice from Mars episodes and marvel about how they... don't hold up as well as you remember. You think you liked Street Sharks better anyway...