Ejemplo n.º 1
0
//////////////////
// Actually parse
void MatroskaWrapper::Parse() {
    // Clear keyframes and timecodes
    keyFrames.Clear();
    bytePos.Clear();
    timecodes.clear();

    // Get info
    int tracks = mkv_GetNumTracks(file);
    TrackInfo *trackInfo;
    SegmentInfo *segInfo = mkv_GetFileInfo(file);

    // Parse tracks
    for (int track=0; track<tracks; track++) {
        trackInfo = mkv_GetTrackInfo(file,track);

        // Video track
        if (trackInfo->Type == 1) {
            // Variables
            ulonglong startTime, endTime, filePos;
            unsigned int rt, frameSize, frameFlags;
            CompressedStream *cs = NULL;

            // Timecode scale
            __int64 timecodeScale = mkv_TruncFloat(trackInfo->TimecodeScale) * segInfo->TimecodeScale;

            // Mask other tracks away
            mkv_SetTrackMask(file, ~(1 << track));

            // Progress bar
            int totalTime = double(segInfo->Duration) / timecodeScale;
            volatile bool canceled = false;
            DialogProgress *progress = new DialogProgress(NULL,_("Parsing Matroska"),&canceled,_("Reading keyframe and timecode data from Matroska file."),0,totalTime);
            progress->Show();
            progress->SetProgress(0,1);

            // Read frames
            int frameN = 0;
            while (mkv_ReadFrame(file,0,&rt,&startTime,&endTime,&filePos,&frameSize,&frameFlags) == 0) {
                // Read value
                double curTime = double(startTime) / 1000000.0;
                frames.push_back(MkvFrame((frameFlags & FRAME_KF) != 0,curTime,filePos));
                frameN++;

                // Cancelled?
                if (canceled) {
                    Close();
                    throw _T("Canceled");
                }

                // Update progress
                progress->SetProgress(curTime,totalTime);
            }

            // Clean up progress
            if (!canceled) progress->Destroy();

            break;
        }
    }

    // Copy raw
    for (std::list<MkvFrame>::iterator cur=frames.begin(); cur!=frames.end(); cur++) {
        rawFrames.push_back(*cur);
    }

    // Process timecodes and keyframes
    frames.sort();
    MkvFrame curFrame(false,0,0);
    int i = 0;
    for (std::list<MkvFrame>::iterator cur=frames.begin(); cur!=frames.end(); cur++) {
        curFrame = *cur;
        if (curFrame.isKey) keyFrames.Add(i);
        bytePos.Add(curFrame.filePos);
        timecodes.push_back(curFrame.time);
        i++;
    }
}
Ejemplo n.º 2
0
void MatroskaWrapper::GetSubtitles(agi::fs::path const& filename, AssFile *target) {
	MkvStdIO input(filename);
	char err[2048];
	agi::scoped_holder<MatroskaFile*, decltype(&mkv_Close)> file(mkv_Open(&input, err, sizeof(err)), mkv_Close);
	if (!file) throw MatroskaException(err);

	// Get info
	unsigned tracks = mkv_GetNumTracks(file);
	std::vector<unsigned> tracksFound;
	std::vector<std::string> tracksNames;

	// Find tracks
	for (auto track : boost::irange(0u, tracks)) {
		auto trackInfo = mkv_GetTrackInfo(file, track);
		if (trackInfo->Type != 0x11 || trackInfo->CompEnabled) continue;

		// Known subtitle format
		std::string CodecID(trackInfo->CodecID);
		if (CodecID == "S_TEXT/SSA" || CodecID == "S_TEXT/ASS" || CodecID == "S_TEXT/UTF8") {
			tracksFound.push_back(track);
			tracksNames.emplace_back(agi::format("%d (%s %s)", track, CodecID, trackInfo->Language));
			if (trackInfo->Name) {
				tracksNames.back() += ": ";
				tracksNames.back() += trackInfo->Name;
			}
		}
	}

	// No tracks found
	if (tracksFound.empty())
		throw MatroskaException("File has no recognised subtitle tracks.");

	unsigned trackToRead;
	// Only one track found
	if (tracksFound.size() == 1)
		trackToRead = tracksFound[0];
	// Pick a track
	else {
		int choice = wxGetSingleChoiceIndex(_("Choose which track to read:"), _("Multiple subtitle tracks found"), to_wx(tracksNames));
		if (choice == -1)
			throw agi::UserCancelException("canceled");

		trackToRead = tracksFound[choice];
	}

	// Picked track
	mkv_SetTrackMask(file, ~(1 << trackToRead));
	auto trackInfo = mkv_GetTrackInfo(file, trackToRead);
	std::string CodecID(trackInfo->CodecID);
	bool srt = CodecID == "S_TEXT/UTF8";
	bool ssa = CodecID == "S_TEXT/SSA";

	AssParser parser(target, !ssa);

	// Read private data if it's ASS/SSA
	if (!srt) {
		// Read raw data
		std::string priv((const char *)trackInfo->CodecPrivate, trackInfo->CodecPrivateSize);

		// Load into file
		boost::char_separator<char> sep("\r\n");
		for (auto const& cur : boost::tokenizer<boost::char_separator<char>>(priv, sep))
			parser.AddLine(cur);
	}
	// Load default if it's SRT
	else {
		target->LoadDefault(false, OPT_GET("Subtitle Format/SRT/Default Style Catalog")->GetString());
		parser.AddLine("[Events]");
	}

	// Read timecode scale
	auto segInfo = mkv_GetFileInfo(file);
	int64_t timecodeScale = mkv_TruncFloat(trackInfo->TimecodeScale) * segInfo->TimecodeScale;

	// Progress bar
	auto totalTime = double(segInfo->Duration) / timecodeScale;
	DialogProgress progress(nullptr, _("Parsing Matroska"), _("Reading subtitles from Matroska file."));
	progress.Run([&](agi::ProgressSink *ps) { read_subtitles(ps, file, &input, srt, totalTime, &parser); });
}