Skip to content

Commit 6692cf8

Browse files
committed
Continue writing mkv tricks page
1 parent 6390798 commit 6692cf8

File tree

3 files changed

+219
-11
lines changed

3 files changed

+219
-11
lines changed
170 KB
Loading
150 KB
Loading

encoding/mkv.md

Lines changed: 219 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,16 @@ an [audio-cutting python script][acsuite.py],
3333
and a [script to change frame numbers into timestamps][f2ts.py].
3434

3535
What we will be doing:
36+
1. Trimming audio to match our VapourSynth video trims
3637
1. Creating mkv files from individual tracks
3738
1. Creating chapters for said files
3839
1. Creating virtual timelines with ordered chapters
3940
1. Using multiple audio codecs in the same 'apparent file'
40-
1. Creating a playall file with ordered chapters
4141
1. Using editions to change between NCOP and OP
42+
1. Creating a playall file with ordered chapters
43+
44+
*Most of the example files can be found in [this repository][mkvtricks]
45+
if you'd like to follow along.*
4246

4347
[toolnix]: https://mkvtoolnix.download/downloads.html
4448
[tsMuxeR]: https://www.videohelp.com/software/tsMuxeR
@@ -48,6 +52,7 @@ What we will be doing:
4852
[mpv]: https://mpv.io/installation/
4953
[acsuite.py]: https://github.com/OrangeChannel/acsuite/blob/04194ad6d4e65e491b5c09219d3f9e832651f2c0/acsuite.py
5054
[f2ts.py]: https://gist.githubusercontent.com/OrangeChannel/330a032e4d6cf9265b8b007c41112937/raw/frame-to-timestamp.py
55+
[mkvtricks]: https://github.com/OrangeChannel/mastroka-tricks-files
5156

5257

5358
## Starting with trimming the video
@@ -120,8 +125,8 @@ Now, either in a new python instance
120125
or from within our VapourSynth script above,
121126
we will use `acsuite.py` to trim our audio,
122127
and generate chapter timings.
123-
(The lines should be run one at a time,
124-
and commented out when not in use. If running from within VSEdit,
128+
(The lines should be commented out when not in use.
129+
If running from within VSEdit,
125130
you must name and save the script first if you do not specify full paths
126131
for the `outfile` and/or `chapter_file`.)
127132

@@ -170,9 +175,9 @@ and MKVToolNix GUI will open the following OGM chapter files:
170175
- [ep2_chapters.txt][ep2txt]
171176
- [ep3_chapters.txt][ep3txt]
172177

173-
[ep1txt]: https://github.com/OrangeChannel/mastroka-tricks-files/blob/cd1968bcb7bd73e720f8822c9c8375a563635e1b/ep1_chapters.txt
174-
[ep2txt]: https://github.com/OrangeChannel/mastroka-tricks-files/blob/cd1968bcb7bd73e720f8822c9c8375a563635e1b/ep2_chapters.txt
175-
[ep3txt]: https://github.com/OrangeChannel/mastroka-tricks-files/blob/cd1968bcb7bd73e720f8822c9c8375a563635e1b/ep3_chapters.txt
178+
[ep1txt]: https://github.com/OrangeChannel/mastroka-tricks-files/blob/b9a079b04efd7e3891c05251a1dc6ccf3a1671ba/ep1_chapters.txt
179+
[ep2txt]: https://github.com/OrangeChannel/mastroka-tricks-files/blob/b9a079b04efd7e3891c05251a1dc6ccf3a1671ba/ep2_chapters.txt
180+
[ep3txt]: https://github.com/OrangeChannel/mastroka-tricks-files/blob/b9a079b04efd7e3891c05251a1dc6ccf3a1671ba/ep3_chapters.txt
176181

177182

178183
### Encoding the audio
@@ -213,7 +218,7 @@ we've encoded only the OP/EDs with FLAC.
213218
This leaves the episode to be compressed more efficiently with a lossy codec
214219
such as opus, for those who don't like 'wasting' space with lossless audio.
215220
Since we are encoding around 90%[^2] of the show with opus,
216-
and only have the OP/ED + NCOP/NCED files occuring once,
221+
and only have the OP/ED + NCOP/NCED files occurring once,
217222
the filesize difference and lossless music should satisfy both groups.
218223
Normal dialogue should be nearly transparent at 128 kbps with opus,
219224
but this comes with some caveats.
@@ -353,11 +358,26 @@ meaning it can be played separately on its own.
353358
[f2ts.py]: https://gist.githubusercontent.com/OrangeChannel/330a032e4d6cf9265b8b007c41112937/raw/frame-to-timestamp.py
354359

355360

361+
#### Episode 2 and 3 Example
362+
363+
*If you are comfortable with how to create ordered chapters now,
364+
you can skip this section.
365+
This is just going to continue the same process above for
366+
episodes 2 and 3.*
367+
368+
Example files can be found in the [repo][] mentioned above.
369+
370+
<!TODO - add some screenshots here of the other episodes' chapters>
371+
372+
[repo]: https://github.com/OrangeChannel/mastroka-tricks-files
373+
374+
356375
### Using editions to add in an NCOP
357376

358377
Similar to adding in the OP,
359378
we will find the timestamp for the NCOP clip.
360-
`ncop = bdmv_NCOP[:2158]` > $$2158-0=2158$$ therefore, `2158 > 00:01:30.006583333`.
379+
`ncop = bdmv_NCOP[:2158]` > $$2158-0=2158$$ therefore,
380+
`2158 > 00:01:30.006583333`.
361381

362382
Using the same *Chapter editor* GUI,
363383
duplicate our first *Edition entry*.
@@ -381,6 +401,13 @@ and select our already muxed `01.mkv`.
381401
On playback, you should now see the OP chapter
382402
inserted towards the end of the episode seamlessly.
383403

404+
To create a playall file later,
405+
it is best to also save these chapters to external xml files.
406+
407+
- `ep1.xml`
408+
- `ep2.xml`
409+
- `ep3.xml`
410+
384411
---
385412

386413

@@ -453,12 +480,193 @@ ep1_A + OP + ep1_B + ep2_A + ep2_B + ep2_C + ep3_A + ep3_B + ED + ep3_C
453480
and the ED occurring at the same point it last happened in the source.
454481
In reality, if we had all 12 episodes to work with,
455482
we could put the ED at the *real* last time it plays
456-
(likely towards the end of ep12).
483+
(likely towards the end of episode 12).
484+
485+
In order to create our playable file,
486+
we need a small mkv in the same format as our other files,
487+
with the tracks in the same order.
488+
A quick way to do this is cropping the entire file with FFmpeg.
489+
490+
```sh
491+
ffmpeg -i ep1.mkv -c copy -fs 5M playall.mkv
492+
```
493+
494+
`-fs 5M` will 'limit' the output file to around 5 MiB,
495+
while `-c copy` ensures all tracks and relevant headers are copied.
496+
497+
If we were to play this file now,
498+
it would likely be the first 1 or 2 seconds of our episode 1.
499+
In order for this file to become entirely a virtual timeline,
500+
*all* of the chapters must be ordered
501+
and point to other files in the folder.
502+
503+
Using the GUI, we can speed up this process somewhat.
504+
In the chapter editor,
505+
open our three chapter files,
506+
`ep1.xml`, `ep2.xml`, `ep3.xml`,
507+
and open a new blank chapter file.
508+
509+
On this new file,
510+
delete the top edition entry.
511+
512+
On our episode 1 tab,
513+
right-click our top *Edition entry*,
514+
and *Copy to other tab* into our blank file.
515+
On our blank file,
516+
we will now need to add segment UIDs to every episode 1 chapter.
517+
We can do this by opening the folder icon to our ep1.mkv file
518+
for every chapter that will be referencing our episode 1 file.
519+
520+
The OP chapter should already have the SUID to the op.mkv file,
521+
so it does not need to be changed.
522+
523+
Sadly, there is no further shortcuts for copying our other two episodes'
524+
chapters into this new blank file,
525+
so they will need to be copied over by hand.
526+
The name, start timestamp, end timestamp, and segment UID
527+
are the only fields we need. (For chapters coming from episode 2,
528+
the segment UID must be from our `ep2.mkv` file, etc.)
529+
530+
![playall chapters without cutting](images/mkv/play_chapters.png)
531+
532+
Once we have all of these chapters ordered and using the correct SUIDs,
533+
we can save this to our `playall.mkv` file.
534+
535+
It is also possible to create a better timeline as stated above,
536+
that skips previews, and only plays the OP/ED once over the entire series.
537+
We will create a copy of our `playall.mkv` file
538+
and rename it `playall-skip.mkv`.
539+
Since we already have a timeline of all (3 currently) of our episodes' chapters,
540+
we only need to cut out the chapters titled *Preview*
541+
and any extra times the OP/ED play.
542+
Since we only have 3 example files,
543+
we'll be playing the OP on its first occurrence,
544+
and the ED on its last occurrence.
545+
546+
![playall chapters skipping the previews and OP/ED](images/mkv/play_chapters_skip.png)
547+
548+
Saving this to our playall-skip file results in the following...
549+
550+
- `playall.mkv - 01:10:57.880` virtual timeline in player
551+
- `playall-skip.mkv - 01:05:43.774` virtual timeline in player
552+
553+
...despite each file only being ~5 MiB.
554+
555+
556+
#### Note about playall files
557+
558+
Although in our example,
559+
we are referencing files that they themselves reference other files,
560+
this doesn't have to be the case.
561+
If you prefer not to use ordered-chapters in your release
562+
for splicing in the OP/ED,
563+
**you can still create a playall file simply referencing your normal files**.
564+
In the same way as the example above,
565+
you can simply remove the OP/ED and previews from your playall timeline
566+
by simply not including those chapter timings from each episode.
567+
568+
569+
### Final notes
570+
571+
Editions mixed with ordered chapters can be used for a myriad of purposes
572+
besides those shown here.
573+
Although these are likely the most common uses in the fansubbing scene,
574+
editions can also be used for shows with multiple 'play orders'.
575+
Ordered chapters can link together multiple seasons of the same show,
576+
or can be used to hide certain sections of a file
577+
(maybe for creating an easter egg in your release).
578+
Similar to how our playall file only plays content from *other* files,
579+
parts of a file can be skipped over
580+
(or in the case of playall, entire files become completely removed).
581+
Obviously, simply removing the chapters will reveal the real tracks
582+
of the source file,
583+
in our case just a couple of frames from our first episode.
584+
585+
Notes about playback issues and possible artifacts:
586+
587+
- Because some subtitle information within Sub Station Alpha files
588+
is [privately stored in the mkv's header][mastroka-ass],
589+
**all styles** and information under `[Script Info]`
590+
that are present in **all** referenced files
591+
(i.e. styles only used in the OP not main episode)
592+
must also be present in the single file containing the chapters
593+
that references the other files.
594+
(i.e. ep#.mkv in our case).
595+
- This means that all relevant styles **must** be included in the playall file for proper playback.
596+
- Fonts however only need to be included
597+
in the file that uses them ** [citation needed]
598+
- Subtitles stretching over ordered chapters boundaries may cause some issues,
599+
including in our playall file.
600+
In our playall file,
601+
a way to circumvent some of this problem
602+
would be to combine all consecutive chapters
603+
from the same file into one large chapter simply named "Episode 1" or similar.
604+
- Audio *should* be (close to) silent at the exact moment
605+
the timeline changes between different files it's referencing.
606+
Although this shouldn't cause sync issues,
607+
an abrupt cut in audio may not be something you'd want in your release.
608+
609+
[mastroka-ass]: https://www.matroska.org/technical/specs/subtitles/ssa.html
457610

458611
---
459612

460-
[^1]: the `fd` utility can be installed from its [GitHub repo][fd]
461613

462-
[^2]: This number is actually much higher as we only encode the music once. Assuming a 12 episode show, with 24 minutes per episode and 3 minutes of OP/ED music in each episode, we would be encoding 255 minutes total, with only 3 minutes (1.2%) being the lossless music.
614+
#### Some boring math
615+
616+
We are going to use some basic assumptions here[^3].
617+
- The series we are encoding is 13 episodes long
618+
- 24 minutes per episode
619+
- with a 90 sec OP + ED that plays each episode
620+
- FLAC audio will be around 950 kbps, opus will be 128 kbps.
621+
622+
**Our first goal will be to keep the release under 12 GB total:**
623+
624+
The FLAC audio track for each episode will be 171 MB,
625+
leaving 752 MB (**4,178 kbps**) for the video.
626+
If we were to encode the audio entirely in opus,
627+
the audio track for each episode will be 23 MB,
628+
leaving 900 MB (**5,000 kbps**) for the video.
629+
630+
Changing from FLAC to opus gives us an *extra 19.7% bitrate for our video*,
631+
at the cost of audio quality.
632+
633+
Instead, if we used ordered chapters:
634+
635+
The total amount of video time decreases from 312 min
636+
to 273 min for main episodes and 3 min for the OP/ED.
637+
(If you are applying heavy filtering to this series,
638+
and encoding time is a important factor,
639+
this would result in a *11.5% reduction in encoding time*).
640+
FLAC for the OP + ED results in 21.4 MB total for our lossless audio tracks.
641+
opus for the main episodes (273 min)
642+
results in 262 MB total for our lossy audio tracks.
643+
Assuming the OP/ED are kept at the same bitrate as the rest of our video,
644+
we are left with 11.7 GB for our 276 minutes of video (**5,660 kbps**).
645+
646+
Using ordered chapters in this case,
647+
gives us an *extra 35.5% bitrate for our video*,
648+
with the benefit of still having lossless audio for the OP/ED music.
649+
650+
**If instead we are aiming to save on space:**
651+
652+
Without using ordered chapters,
653+
and keeping the video bitrate at **4,178 kbps**,
654+
encoding all audio as lossy gives us a *16% filesize reduction*
655+
(12 GB to 10.1 GB).
656+
657+
With ordered chapters,
658+
at the same video bitrate of **4,178 kbps** from before,
659+
we reduce our video tracks to 8.65 GB from 9.78 GB (*11.5% reduction*).
660+
We reduce our audio tracks to 283.4 MB from 2.22 GB (*87.2% reduction*).
661+
Total filesize gets reduced from 12 GB to 8.93 GB, a *25.6% reduction*.
662+
663+
---
664+
665+
[^1]: the `fd` utility can be installed from its [GitHub repo][fd].
666+
667+
[^2]: This number is actually much higher as we only encode the music once. For a 12 episode show, with 24 minutes per episode and 3 minutes of OP/ED music in each episode, we would be encoding 255 minutes total, with only 3 minutes (1.2%) being the lossless music.
668+
669+
[^3]: These calculations can be checked with this [bitrate and filesize calculator script][brate-fsize].
463670

464671
[fd]: https://github.com/sharkdp/fd/releases
672+
[brate-fsize]: https://gist.githubusercontent.com/OrangeChannel/816a87cf760d9be19bde18db8818d4bc/raw/bitrate_filesize.py

0 commit comments

Comments
 (0)