View Full Version : finding forced subs in MKV
ScottJ
11th September 2012, 01:06
I'm trying to write a small C or C++ program that can analyze an MKV file and tell me which subtitle track, if any, has the Forced flag set. The target platform is a Dune media player which runs Linux on a Sigma chip.
I've looked at libmatroska and the specs on matroska.org, but I haven't been able to find a simple enough API to get what I need without a lot of overhead. Really, the code should be as simple as:
open file
foreach track
if track.type is subtitle and track.forced
print track.id
close file
I would like my main body to be literally five or six lines of code like this. But from what I can tell about libmatroska, I need to go through all kinds of EBML tags and other low-level details to get this information.
Am I missing something in libmatroska? Is there a different library with a better-suited API?
Guest
11th September 2012, 01:34
I link in Haali's low-level parser (which you can obtain upon request: see the 'Core parser library' box on the right side of this page http://haali.su/mkv/). Then I can enumerate tracks like this (this is for video but subtitle tracks would be similar):
// Initialize matroska parser.
mf = mkv_OpenEx(&st.base, 0, 0, err_msg, sizeof(err_msg));
if (mf == NULL)
return 1;
/* Find a video track. */
for (i = 0; i < mkv_GetNumTracks(mf); i++)
{
TrackInfo *ti = mkv_GetTrackInfo(mf, i);
if (ti->Type == 1 &&
((!strcmp(ti->CodecID, "V_VC1")) ||
(!strcmp(ti->CodecID, "V_MS/VFW/FOURCC") &&
((char *)ti->CodecPrivate)[16] == 'W' &&
((char *)ti->CodecPrivate)[17] == 'V' &&
((char *)ti->CodecPrivate)[18] == 'C' &&
((char *)ti->CodecPrivate)[19] == '1')))
{
VideoStream_Flag = VC1;
track = i;
break;
}
else if (ti->Type == 1 && (!strcmp(ti->CodecID, "V_MPEG2")))
{
VideoStream_Flag = MPG;
track = i;
break;
}
else if (ti->Type == 1 && !strcmp(ti->CodecID, "V_MPEG4/ISO/AVC"))
{
VideoStream_Flag = AVC;
track = i;
break;
}
}
It's not five lines but it's certainly not excessive.
ScottJ
25th September 2012, 21:34
Well, that looks like exactly what I need, but it's not clear what kind of license that code has, and the guy from Haali has not responded to multiple emails.
Any other suggestions?
Guest
25th September 2012, 22:07
Didn't I email it to you or am I thinking of someone else? If not, I can send it to you. There is no licensing indicated in the source files, so it appears to be public domain. Early on the MKV guys wanted to increase its usage and so having some sample code was necessary and useful. It's not the greatest code but it gets the job done.
If you don't want to use it you can write a basic EBML parser.
ScottJ
25th September 2012, 23:00
Yes you did email it to me (thank you!) but there is the following copyright at the top of each file:
* Copyright (c) 2004-2009 Mike Matsnev. All Rights Reserved.
Which does not sound like public domain to me. I would like to redistribute this library in binary form, at least, and preferably source too, but the license just isn't clear.
Guest
25th September 2012, 23:28
Well he gave it to me and allowed me to use it. I think the copyright means you can't publish it. He and I exchanged emails about some issues in the code and he's perfectly aware that I am using it. If you have asked him for it and he doesn't respond then I guess the situation for you is indeed unclear. There are no restrictions on the binary use mentioned anywhere, but the copyright would prevent you from distributing the source, and I shouldn't have sent it to you. :scared:
The web page says: "The library source is available upon request." It certainly doesn't suggest to me that he doesn't want it to be used! And of course the code for mkvtoolnix, which has presumably much more recent parsing code, is available.
Maybe you can ask about all this at lists.matroska.org.
You can always reverse engineer something based on the EBML parsing.
TheFluff
26th September 2012, 14:22
ffms2 uses the same code and got Haali to grant a BSD-like license for using it. The code is available on the ffms2 SVN: matroskaparser.h (http://code.google.com/p/ffmpegsource/source/browse/trunk/src/core/matroskaparser.h), matroskaparser.c (http://code.google.com/p/ffmpegsource/source/browse/trunk/src/core/matroskaparser.c)
Guest
26th September 2012, 14:52
Thank you for that information, TheFluff. Since it appears that Mike intends a BSD-like license (in fact it appears to be the 3-clause "New" BSD license), I have added that notice to my version 2043 distribution. And I'd like to take the opportunity here to thank Mike for making this code available to the desktop video community.
I also note this page, where a "C core library" is mentioned and a BSD license is stated. Strangely, the link to the code itself is dead. It's not certain that this is the same code that we discuss here, but I think it's clear, especially given TheFluff's information, that the intent is for the code we're discussing to be BSD-licensed.
http://www.matroska.org/node/47
ScottJ
26th September 2012, 21:58
Excellent. This will work. Thanks for the help.
ScottJ
30th September 2012, 22:14
Et voila, MKV Forced Subtitles on Dune (http://scottjohnson.org/wiki/Dune/MKV_Forced_Subtitles).
vBulletin® v3.8.11, Copyright ©2000-2026, vBulletin Solutions Inc.