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