View Full Version : Load subtitles and attachments from mkv?
`Orum
9th June 2018, 07:40
I'd like to be able to do all of the following within an AviSynth script:
Load a subtitle script (SSA/ASS) from a mkv container
Load any attachments (fonts) from that same file
Render the subtitles on the video
However, looking through the subtitling filters (http://avisynth.nl/index.php/External_filters#Subtitling) it doesn't look like any support this. Is there some way to do this that I'm not aware of?
Right now I'm doing an ugly workaround by extracting both the subtitles and attached fonts, installing the fonts, and then loading the subtitles from the external file via TextSub(). This involves a lot of work outside of AviSynth that I'd like to avoid, particularly the full font installation.
Edit: Even if I can't load attachments from the mkv, just loading subtitles would still be helpful.
Yanak
10th June 2018, 06:57
Hello,
For the font installation you can maybe skip it with avisynth+, it can load non-installed fonts (http://forum.doom9.org/showthread.php?p=1808146&highlight=font_filename#post1808146) if you provide the name of the font and the location path, for example :
Subtitle("SomeText", font="Olde English", font_filename="H:\fonts\OldeEnglish.ttf", size=50, text_color=$FF000000, halo_color=color_black)
see : http://forum.doom9.org/showthread.php?p=1801206#post1801206
The link says that once the font is loaded there is no more need to give the name or path so it might be possible to then use it with some subtitle rendering plugins once loaded, maybe making a fake empty subtitle first , maybe only applied on the first frame, then trying to see if your plugins used to render the subtitles can then use the non installed font that was loaded using this trick. ( or creating a function loading the fonts just to trigger it maybe)
I use non installed fonts to render text using avisynth native functions but never tried with subtitles plugins, need some testing i guess.
For the necessity to demux the MKV and extract the subs + embedded fonts i don't know if it's possible directly in avisynth, some others will probably have answers to this.
`Orum
10th June 2018, 07:33
The link says that once the font is loaded there is no more need to give the name or path so it might be possible to then use it with some subtitle rendering plugins once loaded, maybe making a fake empty subtitle first , maybe only applied on the first frame, then trying to see if your plugins used to render the subtitles can then use the non installed font that was loaded using this trick. ( or creating a function loading the fonts just to trigger it maybe)
I'd be quite surprised if this works, as I suspect the font is only "loaded" in the context of Subtitle() itself (and not for TextSub()), but I can give it a try when I get home.
real.finder
10th June 2018, 07:45
attach the fonts with the .ass using aegisub then load it with TextSub/TextSubmod
it work with vsfiltermod and xyvsfilter, other may not since there was some bug that fixed later
Yanak
10th June 2018, 09:23
Just tested this quickly :
LoadPlugin("H:\avs\DSS2mod\DSS2.dll")
Loadplugin("H:\VSFilterMod.dll")
DSS2("H:\test.mkv")
Subtitle("0", first_frame=0, last_frame=1,\
font="Pacific Again", font_filename="H:\Pacific Again.ttf", \
text_color=$FF000000, halo_color=$FF000000)
TextSubMod("H:\testsub.ass")
fake subtitle applied on first frame with transparent colors for text and halo, then using vsfiltermod (version r5 i found in a link not long ago in this forum ).
Probably only adding a space for the text will be enough and then no need for the colors part:
Subtitle(" ", first_frame=0, last_frame=1, font="Pacific Again", font_filename="H:\Pacific Again.ttf")
keeping first and last frame option to not slowdown the thing by doing this on the first frame only, if it have any significant speed impact to start with.
In the .ass file there is this, only thing i done is a manual editing to test the non installed font, was Arial before "Pacific Again" :
[Script Info]
; Script generated by FFmpeg/Lavc57.107.100
ScriptType: v4.00+
PlayResX: 384
PlayResY: 288
[V4+ Styles]
Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding
Style: Default,Pacific Again,16,&Hffffff,&Hffffff,&H0,&H0,0,0,0,0,100,100,0,0,1,1,0,2,10,10,10,0
[Events]
Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
etc...
Seems to work, the script launched in avspmod or MPC-HC displays correctly the non-installed font on subtitles. ( also tried with the font "Pacific Again.ttf" loaded from a totally different path, on another drive just to be sure )
Don't know if i am missing something or doing something wrong or if i am lucky but seems to work ¯\_(ツ)_/¯
StainlessS
10th June 2018, 10:38
Also on Wiki,
Not processing frames plugin with Avisynth AtExit function
This plugin does not process frames. It loads a font into Windows and automatically unloads it after avisynth is done running.
To run code at the end of the script, it registers an env->AtExit procedure.
http://avisynth.nl/index.php/Filter_SDK/Non-clip_sample
AtExit:- http://avisynth.nl/index.php/Filter_SDK/Cplusplus_API#AtExit
`Orum
12th June 2018, 14:46
Yep, seems to work, thanks. The only downside is I still have to extract the subtitles and attachments separately, but I don't think there's a way around that (currently, anyway).
Yanak
12th June 2018, 16:59
Good, at least this is one less thing to deal with, I don't know a way to read and extract directly without passing by the demuxing tool step, nobody answered to this so i doubt it is even possible from avisynth directly.
Probably making a script or batch file that will execute the demuxing using something like mkvextract to get all files needed and then maybe having this script pre-writing the start of .avs script too will make it way easier if you have a lot of those tasks to do...
Inside Staxrip i used to have a small powershell script that allowed me to browse for any non-installed font on my computer and add some watermark text into a video with avisytnh "Subtitle" function, and the power-shell script was reading directly the name of the font ( as it often differs from the font file name itself ) and adding it + the path i browsed the font to my staxrip filter.
Long time i did not mess with this and hard to recall everything i made at this time, but this is a part of the powsrshell script used in staxrip in case it could help if you decide to go this way, at least you have here how to get the font name from the tff file :
$FileBrowse = New-Object System.Windows.Forms.OpenFileDialog -Property @{
Multiselect = $false
Filter = 'fonts (*.ttf)|*.ttf' } # File type(s)
[void]$FileBrowse.ShowDialog()
#Get the font name from the browsed file
if ($FileBrowse.FileNames -like "*\*") {$file = "`"$($FileBrowse.FileName)`""
Add-Type -AssemblyName System.Drawing
$getfontname = new-object System.Drawing.Text.PrivateFontCollection
$getfontname.AddFontFile($FileBrowse.FileNames)
$selectfont = $getfontname.Families[-1].Name
$selectedfont = "`"$($getfontname.Families[-1].Name)`""
$font = "font=$selectedfont, font_filename=$file"
It is probably possible to do something a bit more complex that will first extract the .ass subtitle + the font ( and the cover pictures if there is some or if it's needed ) then read the font file name from the tff file and create a starting avisynth script, well i guess, like i said long time I didn't put my hands in this and my brain is rusty now :p
But probably a simple script or batch file would ease your task by automating the extraction process.
Good luck
vBulletin® v3.8.11, Copyright ©2000-2026, vBulletin Solutions Inc.