Skip to content

Instantly share code, notes, and snippets.

@spajak
Last active February 29, 2024 20:35
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spajak/003f2979e85b1ec27eb89cd111a8fc04 to your computer and use it in GitHub Desktop.
Save spajak/003f2979e85b1ec27eb89cd111a8fc04 to your computer and use it in GitHub Desktop.
Convert/rip DVD to MKV. Encode to MPEG-4 & merge into MKV - all without GUI, only command line tools
# My steps to:
# Convert DVD Video to MPEG-4 in MKV without GUI, using only CLI (Command Line Interface) tools.
# No need for MeGUI, Avisynth, Handbrake etc..
# ------------------------------------------------------------------------------
# Tools needed: `mediainfo`, `ffmpeg` & `ffprobe`, `x264`, `mkvmerge`, `mplayer` (optional).
# Google for them. Use latest versions. Windows tip: avoid Cygwin and get
# the official builds, x64, when possible.
# Before start use `mediainfo` & `ffprobe` and note down informations about the source material:
# streams formats & ids, resolutions, fps, durations, chapters. Probe both `IFO` and `VOB` files.
# 1. DEMUXING
# ------------------------------------------------------------------------------
# Using `ffmpeg` concatenate `VOB` files into full titles, demuxing video and audio
# streams at the same time. Video stream (`MPEG-2`) is simply copied to mpg file
# (without transcoding). Audio stream (`AC-3` with 6 channels) is also copied directly
# without losing quality. Titles 2 and 4 both have just `PCM` 2 channel audio -
# encode them to `AAC` (by default 128kb/s), this is good enough.
& ffmpeg -i "concat:VTS_01_1.VOB|VTS_01_2.VOB|VTS_01_3.VOB|VTS_01_4.VOB" -c:v copy -y -map 0:v "01.mpg" -c:a copy -y -map 0:a:1 "01.ac3"
& ffmpeg -i "concat:VTS_02_1.VOB|VTS_02_2.VOB" -c:v copy -y -map 0:v "02.mpg" -c:a aac -y -map 0:a:0 "02.aac"
& ffmpeg -i "concat:VTS_04_1.VOB|VTS_04_2.VOB" -c:v copy -y -map 0:v "04.mpg" -c:a aac -y -map 0:a:0 "04.aac"
# Explanation:
# `-i "concat:..."` - file input using `concat` protocol.
# `-c copy` - set `copy` encoder - to simply copy selected streams (v for video and a - audio).
# `-y` - overwrite output file without asking.
# `-map IID[:a|v]:SID` - select streams to process, `IID` - input id, `SID` - stream id. Both 0-indexed.
# 2. CHAPTERS
# ------------------------------------------------------------------------------
# Using `mplayer` or `mediainfo` extract chapters timestamps. Look for "CHAPTERS:"
# and "Menu", respectively, somewhere in the output.
& mplayer -identify -frames 0 "VTS_01_0.IFO"
& mediainfo "VTS_01_0.IFO"
# We need to extract times in `HH:MM:SS.mmm` format:
# `mplayer`: "CHAPTERS: 00:00:00.000,00:01:45.500,00:06:21.100,00:11:27.000,..."
# Then prepare OGM chapters file: create it manually, (sample provided for reference),
# or use my Python script [ogm-chapters.py](https://gist.github.com/spajak/1462566cdf6a60319882fbdab72ae554)
# 3. ENCODE
# ------------------------------------------------------------------------------
# Encode video stream to `H.264/MPEG-4 AVC` format.
# Before proper encoding, extract small sample just for test:
& ffmpeg -ss "00:15:11.0" -i "01.mpg" -c copy -y -t 10 "01-sample.mpg"
# `-ss` - start time. `-t` - duration.
# Use CRF method (Constant Rate Factor) - no two pass needed:
& x264 --crf 21 --force-cfr -o "01-sample.264" "01-sample.mpg"
# Standard CRF value is 23. 0 is lossless, 51 - worst quality. Experiment.
# `--force-cfr` is to keep constant frame rate CFR (instead of Variable FR).
# If ready, compress all titles:
& x264 --crf 21 --force-cfr -o "01.264" "01.mpg"
# Tips:
# If you have interlaced video enable interlaced mode; top field first: `--tff`,
# bottom field first `--bff`.
# Resizing & cropping: If needed, use also `--vf crop/resize`. To get crop detection use
# `ffmpeg -i "01-sample.mpg" -vf cropdetect -f null -`. Cropping can be also done with
# `mkvmerge` (recommended).
# 4. MERGE
# ------------------------------------------------------------------------------
# We have now video stream `*.264`, audio stream `*.ac3` or `*.aac`, and chapters `*.txt`.
# So merge it!
$ mkvmerge -o "01.mkv" --default-language "eng" --chapters "01.txt" "01.264" "01.ac3"
# And enjoy watching your video :)
# ------------------------------------------------------------------------------
# My tools:
# PowerShell 6.2.0, Windows 10.
# MediaInfoLib - v19.04
# ffmpeg version N-94063-g86f04b918c Copyright (c) 2000-2019 the FFmpeg developers
# x264 0.157.2969 d4099dd built on Mar 12 2019
# mkvmerge v34.0.0 ('Sight and Seen') 64-bit
# MPlayer sherpya-r38135+gb272d5b9b6-8.3-win32 (C) 2000-2019 MPlayer Team
CHAPTER000=00:00:00.000
CHAPTER000NAME=Chapter 001
CHAPTER001=00:01:45.500
CHAPTER001NAME=Chapter 002
CHAPTER002=00:06:21.100
CHAPTER002NAME=Chapter 003
CHAPTER003=00:11:27.000
CHAPTER003NAME=Chapter 004
CHAPTER004=00:15:32.000
CHAPTER004NAME=Chapter 005
CHAPTER005=00:18:32.500
CHAPTER005NAME=Chapter 006
CHAPTER006=00:24:29.434
CHAPTER006NAME=Chapter 007
CHAPTER007=00:29:01.934
CHAPTER007NAME=Chapter 008
CHAPTER008=00:34:41.501
CHAPTER008NAME=Chapter 009
CHAPTER009=00:39:53.734
CHAPTER009NAME=Chapter 010
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment