Thursday, June 15, 2017

Tutorial - Converting DVDs to editable video files - Option 1


UPDATE: Due to an increase in VirusTotal virus detections on more recent versions of MakeMKV, I'm recommending proceeding with caution when installing it. It might just be false positives, but if you're working on a major project, you might consider other methods for ripping DVDs.

After my last tutorial, I decided to focus on improving my antiquated DVD-ProRes workflow.

Thankfully, newer software can make this process a lot simpler.

A program called MakeMKV works as a sort of all-in-one DVD/Blu-Ray extracting program, and unlike most similar programs, the resulting file is not only all in a single file, but is not recompressed. You get an .mkv wrapper around the video and audio that you choose to extract, and that's about it.

From there, you can use the FFmpegSource filter in AVISynth to load the file, and then apply any additional processing you need. If MakeMKV detects your footage as being 24p, then it will include that metadata in its .mkv, so you shouldn't have to go through the IVTC process. If it detects it as 60i, then you can use QTGMC and it should work... however, the preview window in AVSPmod can be a little glitchy when using FFmpegSource, and scrubbing back and forth may display hitches not present in the rendered end result.

One additional note: FFmpegSource needs to index the file before loading it, so don't worry if it takes a long time to load initially. This index is dumped into an .ffindex file in the same folder as your video file, which will load automatically in the future.

I should also mention that this process doesn't work on all DVDs. Some will have audio sync issues, and need an alternate (and more complicated) workflow with its own benefits and tradeoffs that I'll outline in a future post.


BIG DISCLAIMERS: 

This process may not work, may crash, or do other things to your system. I check for viruses before using any software, but malicious hackers have been known to break into developer accounts and insert code into benign programs.

You should always obtain the written permission of the copyright owner of the content before decrypting their DVD. Failure to do so may result in fines, lawsuits and/or jail time depending on your country's laws.

You have been warned. 

Also, this tutorial is for Windows 10. Most of the steps work for previous versions of Windows, but may require slight modifications. Users of MacOS and other OSes should look elsewhere.

UPDATE: I have a new version of the AVISynth and FFMPEG portion of this tutorial that is more stable, faster, and even potentially higher quality. Please check that out first if you're setting up those programs for the first time.

Here's the video version of this tutorial:





And here's where you go to get the software:

MakeMKV

AVISynth (You will need both the 32-bit "Official" build and the 32-bit "Unofficial" multi-threaded build)

FFmpegSource

If you're going to be dealing with 60i footage, then you'll probably want to deinterlace with QTGMC. If you need to know how to do this, check out my previous blog post.

QTGMC (Get the QTGMC download, then get both the "required" and "optional" filters as listed on the page. If there's a choice, get the 32-bit [x86] version of filters.)

LimitedSharpen (Technically LimitedSharpenFaster. Optional. Get the x86 version in order for it to work within a script using QTGMC. )

FFMPEG Windows Binaries (Get the current 32-bit static version)

AvsPmod

Setup:

  1. Install MakeMKV.
  2. Run the main AVISynth installer. 
  3. Put the AVISynth filters you downloaded (.dlls and .avsi files) in the AVISynth Plugins directory. Make sure to use the 32-bit (x86) versions of any filters if there is a choice. Make sure you're using version 2.2.7 (or later) of MaskTools2.
  4. Copy the multithreaded build of avisynth.dll to your system folder (Usually C:/Windows/SysWow64/) and replace the existing file.
  5. Copy the libfftw3f-3.dll file your system folder.
  6. Extract FFMPEG and AvsPmod to their own folders. Keep the folder with the ffmpeg.exe binary open.
  7. Set up FFMPEG to run from any directory on your PC by adding it to your PATH variable:
    1. Press the Windows and R keys.
    2. Type "control sysdm.cpl,,3". Click "Run".
    3. Click on "Environment Variables".
    4. Select "Path" under "System variables" and click "Edit".
    5. Go back to your open folder where ffmpeg.exe is located. Select and copy the folder's path address from the address bar towards the top of the window.
    6. Back in the "Edit environment variable" window, click "New" and paste in the folder path. Click OK on all the windows you opened to get here.

Process:

Open MakeMKV.

Put a DVD in your optical drive. Open the DVD in MakeMKV.

Note: I've run into a DVD sent by a client that had some odd authoring, which prevented me from opening it successfully in MakeMKV. I ended up using my older method to process it, but there's now an option that gets around the problem in the last two betas of MakeMKV. Before clicking on the giant drive icon, check the "Open DVD Manually" box, then when you get a list of the disk contents, type each of the titles you want to load in the text box, separated by a space between them. Hit OK and those titles should now open up properly. Also, you might need to decrease the minimum length of a detected title in MakeMKV's preferences.

Select the Titles and Audio tracks that you want to copy over. Select the output directory. Click on the Save to MKV button.

When done, close MakeMKV and go to the directory you just saved the .mkv file to.

In the same directory as your .mkv file, make an .avs script with the settings listed below. Change the filename, source filter, and crop settings as necessary.

Here's my boilerplate .avs script settings:

SetMTMode (5, 10)
FFmpegSource2("videofile.mkv", atrack=1)

If you're using 4:3 video, then a pixel aspect ratio conversion is necessary (unless you'll be outputting back to DVD...in which case, ugh):
BilinearResize(720,540)

And here's what to add if you want to upscale to 720p HD:
BilinearResize(1280,720)

You can also use Lanczos4Resize or Spline64Resize on the last resize step if you need some overall sharpening. I'll also sometimes use LimitedSharpenFaster at the end for a little extra punch.

LimitedSharpenFaster()
Please keep in mind that DVD video tends to have lots of compression artifacts, and sharpening can accentuate them.

Working on this script in AvsPmod will let you preview your results, and change them to your preference. When you're done, don't forget to save your work.

Create a new text file in the same directory and change its extension to .bat. Add the FFMPEG commands of your choice. For example, here's a command to encode an .avs script to a ProRes 422HQ Quicktime file:

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

Change where necessary. The quotes around the filenames in the FFMPEG command allow you to enter path addresses and filenames with spaces in them.

You might notice i'm using "prores" instead of "prores_ks" for the video codec setting. Since DVDs are already very heavily compressed, the difference in visual quality between the two encoders isn't enough to justify the massive difference in speed. If you're really keen on getting every last little detail, then "-c:v prores_ks -profile:v 3 -qscale:v 5" will provide it at the cost of a much larger filesize and a 2-5x longer rendering time. Also, the resulting file is nonstandard, so (I have been told) it might be bounced by the QC department of some TV stations.

Saturday, April 15, 2017

Tutorial - An (outdated) SD interlaced to HD progressive Conversion Tutorial, or A Long-delayed Video



Hey folks - just wanted to let you know that this tutorial is now officially outdated.  I'm leaving it up here are for up historical interest, but here's a link to a post that I will maintain from this point forward with the current instructions for setting up AVISynth and QTGMC:



============================================




After all the time (see here and here) I've put into getting the AVISynth+QTGMC+FFMPEG deinterlacing workflow working properly on my system, I figured it was about time for a video tutorial. In my spare time, I've been trying to record one.


It's taken a lot longer than I thought.

Partly, this was due to finding new tricks/plugins that improved the process. Partly, it was due to finding unexpected bugs.

However, the main delay came from me not just wanting to do a "do this, then this' tutorial where you come away with no context whatsoever about what's actually happening. I want people who watch the video to get a good idea of what AVISynth is, and what FFMPEG brings to the process.

So, I'd start recording a tutorial, then get caught up in a tangent. An hour and a half later, I still haven't finished the video.

Plus, I make mistakes. There's a lot of material to cover, and I found myself leaving out crucial points (or ones that clear up confusing areas). I could have tried writing out a script, or just patching over my mistakes. However, it turned out that just repeating the actions so many times that you can practically do them in your sleep eventually got me to the right place. I still had some audio issues to address in post, but my overall presentation quality is much improved from earlier attempts.

Here's a written description of the process, including links:


Friday, April 14, 2017

Deinterlacing HD footage with FFMPEG (Outdated)

Hey there folks - this post is pretty old at this point, and many options for deinterlacing have changed. Check out this latest post where I compare a few of the options:


I've kept this post up mainly for archival value.

After my last post, I played around with bunch of different settings, and discovered that plain old FFMPEG can do decent deinterlacing. One of the reasons I looked into this is that QTGMC (my AVISynth deinterlacing plugin of choice) is ridiculously slow and prone to crashing when processing HD footage.

The problem with FFMPEG is that its documentation is enormous, and while it sometimes includes clear examples to show you how to use certain features, other times it's simply assumed that you're technically savvy enough to know what they're talking about. For example, trying to figure out the best method for deinterlacing involved me doing Google searches for each of the various methods. Normally, something like that would give results pretty quickly, but most of the substantive discussions were from at least five to nine (!) years ago or related to programs that use FFMPEG as a back-end engine, rather than as a standalone app. Even after all that, it wasn't immediately obvious which method would be superior, so I decided to run a few tests.


Method 1: kerndeint - a relatively simple deinterlacing method that automatically halves the framerate and is roughly on par with the old After Effects / Premiere Pro deinterlacer.

Method 2: w3fdif - Developed by the BBC, this is much closer to what I was looking for. Unfortunately it was originally designed for interlaced standard definition PAL content, and as such somehow does not have a mechanism for selecting Top Field First field order. It did successfully deinterlace the footage, but ended up looking herky-jerky as a result of the field order mismatch.

Method 3: nnedi - The method that QTGMC was designed to incorporate/replace. In theory, it does many of the same things, but to say that trying to figure out the options was confusing is an understatement.

Method 4: bwdif - To quote the FFMPEG documentation: “Motion adaptive deinterlacing based on yadif with the use of w3fdif and cubic interpolation algorithms”. This turned out to be the perfect solution. The default settings automatically detect field order (if it's set in the file's metadata) and do not introduce weird image artifacts, but still give decent deinterlacing. Oh, and it runs significantly faster than AVISynth+QTGMC. Like, 6-8 times as fast.

Now, there are some downsides to bwdif. QTGMC can introduce some ghosting artifacts (including misaligned chroma artifacts), but it can also pull more detail out of the original image. This makes bwdif a sub-par solution for SD interlaced to HD progressive upconversion. FFMPEG also doesn't have the (relatively) easy-to-read syntax of an AVISynth script, and definitely lacks the versatility of the insane range of plugins available for the latter.

As always, here’s an example of the command-line options so you can run this process on your system. Note that you have to use -vf before naming the filter in quotes:

ffmpeg -i "input_video.avi" -vf "bwdif" -c:v libx264 -preset slow -crf 18 -pix_fmt yuv420p -c:a aac -b:a 320k "output_video.mp4" 

This is what I used to deinterlace a 60i HD file and re-encode it for YouTube. For ProRes encoding, see my previous post.

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 ...