Monday, June 10, 2019

AVSMeter - an essential tool for working with AVISynth

As part of the process for making tutorials about various AVISynth options, I often have to benchmark different settings and/or plugins. An easy way to do this is to simply run .avs scripts through FFMPEG.

However, sometimes this doesn't really show how efficient a script is being or not. If there's a bottleneck happening somewhere in the script that's holding you back from better performance, it can be hard to isolate it from the overhead that FFMPEG introduces. Plus, I don't really need to make a new rendered video file for every change in options when I'm trying to benchmark plugins.

Thankfully, there's a better way. The ever-awesome Groucho2004 has written a tool for this purpose called AVSMeter. It has pretty much everything that you would want in an AVISynth benchmarking tool, but it also serves as one of the best AVISynth diagnostic tools as well.

It has both a 32 and 64-bit option so you can compare between different versions of AVISynth, measurements of everything from memory usage to number of threads used. It has a function for checking your AVISynth installation to make sure that both the core program and all plugins are set up correctly. And of course, like any good benchmarking tool, it has comprehensive logging - including the ability to log to .csv files for you spreadsheet folks.

The included documentation does a really good job of explaining all the different features of AVSMeter, so I'm just going to post up a tutorial video about how to install and use it below and leave it at that.

BIG DISCLAIMER: This process may not work, may crash, or do other things to your system. Virus scanning all files is strongly encouraged, but not a 100% guarantee of safety.

You have been warned.




Friday, May 10, 2019

On ergonomics and carpal tunnel syndrome: a dictated post.

This blog was started as a way for me to talk about my experiences with video editing. I've posted up a bunch of different tutorials over the years, workflow tests, and introductions to different pieces of software.

However, the one thing that I haven't done is talk about the physical toll of working (and playing) on a computer all day.

I've developed issues with my posture over the years that have aggravated some lower back problems. This isn't the end of the world, as doing a few simple exercises can strengthen muscles that can counterbalance this effect. I'm working on doing just that right now.

Unfortunately, one area where not taking care of yourself can be a real problem is your wrists. For those unfamiliar with the condition, carpal tunnel syndrome is a RSI, or repetitive stress injury. It's primarily due to grasping for long periods of time and/or performing repetitive motions with your fingers like typing. The symptoms start with swelling and soreness in your wrist and hands, then progress to tingling and finally numbness

I first started having symptoms of carpal tunnel syndrome right around the time I entered college, and it was primarily due to playing video games and working on the computer for long hours. Probably my biggest regret in life is that I didn't take it seriously enough.

Most people who develop carpal tunnel syndrome symptoms recover entirely through a brief treatment of anti-inflammatory drugs and rest. However, if you continue the damaging behavior while you are recovering, the injuries can compound, and you can end up with a condition like mine where it never really goes away.

Thankfully, my symptoms have never gotten bad enough to require surgery, but they've held me back from doing a number of different jobs that could have made me a lot more money than I'm currently making. About a month ago, my symptoms started getting significantly worse. It turns out that holding up in iPad for hours at a time can be just as bad as performing a death grip on a mouse.

So, I find myself back in the same position again.

In the past, I've tried all of the noninvasive treatments for carpal tunnel syndrome. I've done the course of anti-inflammatory drugs, used a wrist brace, even tried acupuncture. The wrist brace didn't really help, the anti-inflammatory drugs helped alleviate the immediate symptoms, but not the overall condition. Acupuncture and massage helped about as much as the drugs.

However, the one bright spot is that the technology that in some ways has contributed to the condition in the first place is now helping me to reduce the repetitive motions that aggravate the condition.

I'm talking about speech-to-text software.

Back in the early-mid 2000's, I used a program called Dragon NaturallySpeaking from time to time. It was expensive, error prone, and slow to use. Dragon is still around, and is probably much improved in its current version, but thankfully I don't need to buy it anymore. Windows 10 includes speech-to-text-software by default. It works almost exactly the same as Dragon, and it's essentially free.

It's also what I'm using right now to write this blog post (as well as the previous post).

Unlike voice assistant transcription capabilities that are available in Android and iOS, Windows Speech Recognition doesn't require an Internet connection, is relatively quick, and doesn't timeout after a certain period of seconds and/or words. It's definitely not perfect, but by having a local dictionary to pull from, you can add and/or retrain words to match whatever subject matter or writing style you happen to use.You also have less of a worry that your voice will be uploaded to some distant cloud storage server and kept there for eternity.

It definitely takes some getting used to, but I find that I can write blog posts like this and all sorts of other documents fairly easily. It doesn't help with entering FFMPEG commands or writing AVISynth scripts, but it allows me to work on projects that otherwise would have been detrimental to my health.

Speaking of health, I'm currently looking into other treatments that could significantly improve my condition. If that works, I'll either update this post or make a new one. However, for anyone who's currently suffering with carpal tunnel syndrome. I want to let people know that there are ways to get back some of the functionality that you had in the past via speech-to-text software.

Wednesday, May 8, 2019

Why I don't (generally) like to use wrapper programs

Most of the comments on my videos and blog are questions about particular workflow issues. However, one of the most repeated questions outside of that has to do with why I don't use/recommend using programs like MeGUI or Hybrid.

The simple answer is that I don't trust that they'll stay around. The history of open source software is littered with wrapper programs that were really convenient for a brief moment in time, until life or lack of interest led to the (usually solitary) developer abandoning the project. Sometimes, someone else will come along and rescue it. More often than not, though, it's just left to rot.

This means that if you base your entire workflow around one of these programs, there's a better-than-average chance that at some point you'll either have to hunt for a replacement that doesn't exist, or learn how to use the programs that underlie the wrapper. After getting burned a few times by this, I have chosen the latter.

Now, this is not to say that I have an inherent prejudice against all wrapper programs. gMKVExtractGUI saves a ton of time and typing when trying to extract audio from an .mkv file, and is directly recommended by the MKVToolNix devs. My main concern is that if you start by learning wrapper programs and don't really understand the fundamental way that the underlying programs work, you'll be lost at sea if something goes wrong.

Nowhere is this more true (in my opinion) than in the case of FFMPEG.

There are a truly massive number of wrappers for FFMPEG. Most are designed to convert consumer video files into a format that works better for media players or phones. Some are free, while quite a few others are scammy $15-$50 converter programs that spam advertisements on places like Quora. A few are actually designed for professional usage, with their primary function being to add ProRes encoding to Windows (which is not as much an issue since Adobe added official ProRes encoding to Creative Cloud video apps).

Now, I want to make a distinction between wrapper programs and programs that merely incorporate FFMPEG. Programs like Handbrake, ShotCut and KDEnlive all use FFMPEG for exporting video, but don't exclusively rely on FFMPEG code to function. They are also projects with a long development history, dedicated teams of people who work on them, and large numbers of users.

Here's the thing, though. FFMPEG goes through significant development, sometimes leading to new features and important bug fixes. Its main ProRes encoder in particular has had major bug fixes within the last year, as I have posted elsewhere on this blog. Most wrapper programs -- and other programs that rely on FFMPEG -- don't update their included version of FFMPEG on a regular basis. In some cases, you might be using a version that's several years out of date. If you learn how to use FFMPEG just by itself, then you can update whenever you want, and potentially troubleshoot issues that the wrapper program hasn't fixed yet.

So, basically, that's my thought process. I completely understand that many people would prefer an all in one solution, especially for programs like AVISynth where the setup process is definitely not trivial. I just tend to gear my tutorials towards a sort of "manual transmission" philosophy because I believe that in the long run it's more helpful.

Wednesday, February 20, 2019

Motion Interpolation plugins in AVISynth (WIP)

(This post is a work-in-progress. I'll be adding screengrabs, more info and eventually a video tutorial. Keep checking back if you'd like to see more, and feel free to comment if you think I've left something out.)

I'm not generally a fan of motion interpolation. I especially hate the "motionflow" BS on most modern TVs. The only time I really like using it is with QTGMC, and that's because it's helping to recreate full frame detail from half-height fields.

However, I realize that motion interpolation can be useful in other situations. In addition to allowing you to make a slow motion shot out of footage shot at a regular framerate, you can also use it to do framerate conversions without changing overall runtime, such as going from 29.97fps to 23.976fps.

There are a number of different options for performing motion interpolation. As far as I can tell, Twixtor is pretty much the gold standard for "run it yourself" software in pro circles, but it has the notable limitation of not being able to detect scene changes. That means that if you're working on more than one shot, you would need to apply the Twixtor effect to every *individual* clip in a timeline in order for it to work properly rather than just applying it after exporting. However, Twixtor is really good at what it does, and motion is less choppy overall than other solutions.

Unfortunately, Twixtor is also $395, or closer to $600 if you want the "Pro" version that has more features for VFX artists.

So what if you're broke, or don't use one of the editing programs that Twixtor supports? As it turns out, AVISynth does actually have some interesting alternatives. They have their own issues, but they can definitely get the job done.


Update: Here's a rough draft of the video tutorial for setting up and using the plugins. I plan on releasing a more comprehensive video later, but if you'd like a preview, check it out:




FrameRateConverter requires MVTools2, MaskTools2, and the fftw3 library, just like QTGMC. *Unlike* QTGMC, however, if you want to use the 64-bit version, you'll need to install the 64-bit version of fftw3 in your System32 directory.

It has a wide range of supported colorspaces, comes in both 32 and 64-bit versions and the quality is good overall. However, the speed ranges quite a bit depending on the settings used.

Using a script for converting a 29.97fps HD ProRes file to 59.94fps, I got speeds ranging from totally unusable (32-bit version, "Slowest" preset, which estimated almost 3 hours to finish) to 1:37 (64-bit version, "Faster" preset). The 32-bit version was exponentially slower than the 64-bit version at slower presets, and inconsistent at faster ones, sometimes varying by several minutes. By contrast, the 64-bit version uses more memory, but is faster and has almost no variation in run times.

As to visual quality, FRC looks like it uses a form of partial frameblending, which can make some motion look a little less smooth than Twixtor (or the other filter I'm looking at below). On the plus side, it does have scene detection, and there is little-to-no warping of the image.

Here's a sample script (AVS+ syntax):

SetFilterMTMode("FrameRateConverter", 2)
FFmpegSource2("The Hollar Commercial 1 - ProRes422HQ.mov", atrack=-1)
FrameRateConverter(Preset="Slow")
Prefetch(10)

One thing to note is that if you're not doing a straight frame-doubling, you have to use a somewhat odd system for specifying framerate. You have to specify the "NewNum" and "NewDen",  which corresponds to a numerator and a denominator, somewhat similar to how FFMPEG handles framerates. For example, if I wanted to convert to 59.94fps, I'd use a line like this:

FrameRateConverter(Preset="Slow", FrameDouble=false, NewNum=60000, NewDen=1001)

Likewise,

NewNum=24000, NewDen=1001 

would get you to 23.976 and

NewNum=30000, NewDen=1001 

would convert to 29.97. Keep this syntax in mind, because it's how the next filter works as well. Speaking of which:



Interframe requires SVPFlow, a version of which is included in the download.

It only works in YV12 colorspace, doesn't have a 64-bit version, *really* doesn't like working with FFMPEGSource when using 10-bit material (even after colorspace conversion), and the "beginner's guide" for it focuses on using MeGUI. Oddly enough, it also has settings for stereoscopic 3D content.

However, it absolutely mops the floor with FrameRateConverter when it comes to speed, and it doesn't have the frameblending artifacts.

Using the slowest preset (which is called "medium", for some reason), it ran anywhere from 19-40fps. You can enable GPU support, but this doesn't actually speed up the preset. Instead, it supposedly works harder to give better results. In practice, I don't notice a massive difference, but you can try it out for yourself and see which settings you prefer.

Here's a sample command:

SetFilterMTMode("InterFrame", 2)
QTInput("The Hollar Commercial 1 - ProRes422HQ.mov", audio=1)
ConvertToYV12()
InterFrame(Preset="Medium", Tuning="Smooth", GPU=True, Cores=10)
Prefetch(10)

If your input video is in 10-bit color, you'll need to use an input filter other than FFmpegSource2. In this case, I'm using the massively outdated QTInput. If your input video is in 8-bit color, you don't need the ConvertToYV12() command, and FFmpegSource2 should work just fine.



Overall, the following distinction applies:

Interframe preserves more detail in "Weak" mode, but objects in motion can have wobblier edges. In "Smooth", motion looks smoother, but less detail is preserved. Both modes can have garbled edges around moving high-contrast edges.

FrameRateConverter has a more "frameblended" look, with some choppier/blurred movement, but almost no edge artifacts. It can have issues with certain patterns of detail in some shots, though. It generated a "twitchy" artifact around the edge of a corrugated metal roof during a panning shot in one video I used it on.

Twixtor is like a more refined version of Interframe, with better edge cohesion, but still some noticeable artifacts at default settings. In particular, it has a larger "warped" area.



There are a couple of other options I've played around with, but none have been as good.

YFRC is a script that requires MaskTools2 and MVTools. Like some of the other options, it needs YV12 colorspace. On the plus side, it supports common framesizes from 320x240 up to 1920x1080, although you have to set the block size appropriately. It also supports multithreading and 64-bit mode. You can change the framerate conversion method by changing OverlayType. OverlayType=0 uses frameblending, while OverlayType=1 doesn't blend frames, but can be choppier as a result as it doesn't fully interpolate between frames in many cases. Neither is as good overall as Interframe or FRC, but YFRC does have the best scene detection of any of the options available, with no blended/interpolated frames between cuts.

ChangeFPS is the default framerate conversion command in AVISynth. It isn't multithreaded, but does work in 64-bit mode, and can use whatever colorspace you throw at it (at least in AVS+). Unfortunately, it's about the same as using the basic framerate conversion tools of Premiere Pro/Resolve/etc, which is to say it looks pretty choppy. It does no motion interpolation, instead simply adding or removing frames as necessary.

ConvertFPS has most of the same benefits of ChangeFPS, but can either do frameblending "Blend" mode (the default) or a "Switch" mode that looks like the resulting video was captured from a game with Vsync disabled. The Switch mode also requires 8-bit YUY2 colorspace, so you'll have to do those conversions first. I don't recommend using the Switch mode.

MSU_FRC does an okay-decent job, but is limited to YV12 colorspace *and* base16 block sizes, so it only really works with SD video frame sizes. It's also not multithreaded or 64-bit capable, which makes it slooooow.

FrameDbl only does straight framedoubling, does so via blending frames, and does not use multithreading. Also, it has the same issue with 10-bit video in FFMPEGSource as Interframe.

Motion is ancient, has confusing documentation, is not multithreaded (in fact, multithreading slows it down significantly), and produces nasty-looking artifacts.

Finally, there's the minterpolate filter in FFMPEG. Using the recommended settings from the FFMPEG Wiki, I came up with a command like this:

ffmpeg -i "input.mov" -filter:v "minterpolate='mi_mode=mci:mc_mode=aobmc:vsbmc=1:fps=60'" -c:v prores_ks -c:a copy "output.mov"

The end result is very smooth and has excellent scene detection, but with a more distorted "warped" area around moving objects, and more "blockiness" in those areas. It's also glacially slow; neither multithreaded nor GPU-accelerated. There's a very minor speedup from using the 64-bit version in my limited testing.


Friday, January 4, 2019

Tutorial - An alternate workflow for transcoding DVDs to editable video files

MakeMKV has been my go-to solution for DVD extraction for a while, but it sometimes can create files that have issues when loaded into AVISynth. In those cases, there a couple of things you can try to fix the issue, or you can use a different workflow altogether.

Briefly, the steps for this alternate workflow are as follows:

  • Copy over the DVD's files (if DVD is unencrypted) or use a decryption program and point the output to a directory on a local drive.
  • Determine what title you want to convert.
  • Open up the appropriate .vob files in DGIndex. Save project and demux audio.
  • (Optional) Convert audio to .wav using FFMPEG or Audacity with FFMPEG add-on.
  • Create AVISynth script
    • Load DGIndex project file
    • Load audio
    • Mix audio and video
    • Perform any additional video processing
  • Render out avs script to ProRes file using FFMPEG

It's a bit of a hassle, but I know from experience that it works.


BIG DISCLAIMER: This process may not work, may crash, or do other things to your system. Virus scanning all files is strongly encouraged, but not a 100% guarantee of safety.

Circumventing lawful copy protection is illegal in the United States and many other countries. If you do not have explicit, written permission of the content owner to rip their DVD, you may be committing a crime. Check your local laws to be sure. I am not a lawyer.

You have been warned.

Also, this tutorial is for Windows 10. Most of the steps should work for other OSes, but I won't cover the differences here.

Here's a video version of the tutorial, with some extra info about using MKVToolNix to extract A/V data from MakeMKV DVD rips in order to work around some glitches:

Please note: the 32-bit only limitation mentioned in this video is no longer true. You can ignore that part and essentially follow the process from DGIndex forward in 64-bit AVISynth+.



First, if you haven't already set up AVISynth+ or FFMPEG, check out the following tutorial:

https://macilatthefront.blogspot.com/2018/10/alternate-setup-for-ffmpeg-alternate.html

If you want to install the 64-bit version of AVS+ instead, check out this updated tutorial (that uses VirtualDub2 for rendering, but you can grab 64-bit FFMPEG and use that instead if you like):

https://macilatthefront.blogspot.com/2021/01/deinterlacing-with-avisynth-and-qtgmc.html


When that's done, grab DGMPGDec:

http://rationalqm.us/dgmpgdec/dgmpgdec.html

Depending on the type of DVD you're looking to convert, you might also need:

http://avisynth.nl/index.php/Decomb

for working with 24fps content, and QTGMC for interlaced content. For QTGMC setup, see my tutorial posts above.

Virus scan all downloaded files. 

Extract the DGMPGDec archive into a directory on your system, maybe named "DGMPGDec". If you can't decide where to put the folder, Program Files (x86) or your user folder work well. Copy the DGDecode.dll file to your AVISynth+ plugins+ directory (The main version is 32-bit, but there's a 64-bit now included in the same archive if you want to go that way, which you would put in plugins64+ instead).

Create a folder somewhere on one of your drives for the DVD files, named whatever you want. Put a DVD in your drive.

If the DVD isn't encrypted, just copy over the VIDEO_TS folder or the .VOB files contained within to your new folder.

If it is encrypted, you'll need to use a decryption program of some sort. Due to the various legal issues involved with DVD ripping programs, I'm a little reluctant to link to any specific programs (MakeMKV is technically illegal in some countries as well, but makes some concessions, such as not removing certain types of copy protection on Blu-ray rips that prevent them from being remuxed to a new Blu-ray disc image), and I would caution you to be careful - in addition to my warnings above, semi-to-fully illegal software is a common target for malware authors to try to inject code or compromise the websites of said software in some way.

Regardless, once you have your unencrypted .VOB files copied over, you'll need to find the title you want to transcode.

Often, this is made up of the largest similarly-named files on the disc, but if you don't know what title contains your desired content, use VLC to play back the .VOB files until you find the correct one.

When you know what title you want, open DGIndex from your DGDecode folder. Go to the Audio menu, select Output Method and then Demux All Tracks. Go to the File menu, then select Open. Browse to the folder with your DVD files. Select all the .VOB files in a particular title. They will usually be named with VTS_(title number)_(part number).VOB. For example:


Notice that I don't select the "_0.VOB" files - they're not needed in this case. Click Open, and then you'll get a "File List" window. Confirm that you have all the files you need, or add any you missed. When finished, click OK.

Drag around the playback bar to make sure the entire title is present. If you'd like to get a sense of what DGIndex thinks your video's framerate/aspect ratio is, hit the F5 key and preview it for a minute or so (you can stop playback with the ESC key). When satisfied, go to the File menu and select Save Project. I generally just save it in the same directory as the DVD files, and will proceed as if you have done so.

One thing to look out for: if you get a box like this:


Generally, I will click "No". However, if the resulting project file gives you an "access violation" error during preview or encoding later in the process, then you might need to go back and re-save the project file, than click Yes. (Thanks to C. Sandor for reminding me about this).

Anyways, Save Project will create a .d2v file and at least one audio track in the same directory.

If you'd like, you can convert the audio file to a Wav file ahead of time using FFMPEG like so:

ffmpeg -i "audio.ac3" -c:a pcm_s16le "audio.wav"

The resulting file should work in editing programs either on its own or muxed with the video file we'll be creating in a moment. If the original file has more than 2 channels, they may not be in the correct order is you just use the command above. If you need to mix down to stereo, a common suggestion that I've used is the -ac 2 option, like so:

ffmpeg -i "audio.ac3" -c:a pcm_s16le -ac 2 "audio.wav"

Alternatively, you can use the NicAudio AVISynth filter to import the AC3(or DTS) file, although I've sometimes run into sync issues when using it.

Create a new text document, then change the extension to .avs. Double-click to open it in Notepad or AvsPmod.

Add the following to the newly-created avs file:

video = MPEG2Source("yourproject.d2v")
audio = WavSource("audio.wav")
AudioDub(video, audio)
Spline64Resize(1280, 720)

The above assumes you want to upscale to 720p from an anamorphic widescreen DVD. If your video is 4:3 aspect ratio, then you can use

Spline64Resize(720, 540)

instead to correct for the pixel aspect ratio difference between NTSC video and modern displays.

If you want to import the .ac3 file directly, install the NicAudio plugin into your AVS+ plugins directory and use:
video = MPEG2Source("yourproject.d2v")
audio = NicAC3Source("yourproject T80 2_0ch 192Kbps DELAY 0ms.ac3")
AudioDub(video, audio)
Spline64Resize(1280, 720)

Change as necessary. Again, use at your own risk, as it can cause sync issues.

For 24fps DVDs, you'll need to use the Decomb filter to perform an "inverse telecine" to extract out the 24 progressive frames from the 29.97fps interlaced video.

video = MPEG2Source("yourproject.d2v")
audio = WavSource("audio.wav")
AudioDub(video, audio)
AssumeBFF()
Telecide()
Decimate(cycle=5)
Spline64Resize(1280, 720)

For interlaced video, you'll need to use QTGMC:

SetFilterMTMode("QTGMC", 2)
video = MPEG2Source("yourproject.d2v")
audio = WavSource("audio.wav")
AudioDub(video, audio)
AssumeBFF()
QTGMC(Preset="Slower", FPSDivisor=1, EdiThreads=3)
Spline64Resize(720, 540)
PreFetch(10)

Your EdiThreads and PreFetch settings depend on your processor. Mine has 6-cores with Hyperthreading, for a total of 12 "logical processors". EdiThreads should be set to half your physical cores or less, PreFetch to 1-2 less than your logical processors. FPSDivisor as included in the above command does nothing, letting the framerate be doubled to 59.94fps. However, if you change the 1 to 2, it will divide the framerate in half, resulting in a cleanly derived 29.97fps. Match the framerate to the project you're importing the video into, or the deliverables spec for your ultimate destination.

When you're ready, create a new text file in the same directory, then change the extension to .bat. Right-click to open it, then type something like the following:

ffmpeg -i "your.avs" -c:v prores -profile:v 3 -c:a pcm_s16le "output.mov"

If you're using a DTS file for audio, then -c:a should be pcm_s24le. If you're using DNxHR/HD for your output codec instead of ProRes, you may need to add a colormatrix conversion to avoid a color shift bug on SD-HD upscales:

ffmpeg -i "your.avs" -vf "colormatrix=bt601:bt709" -c:v prores -profile:v 3 -c:a pcm_s16le "output.mov"

If you need more info on colorspace conversions using pix_fmt, check out the ProRes section at the following link:

https://trac.ffmpeg.org/wiki/Encode/VFX

And that's it. Simple, right? ;)

Which deinterlacing algorithm is the best? Part 1 - HD interlaced footage

Before I began, I'd like to give a special thanks to Aleksander Kozak for his help in testing and providing the HD footage used in this ...