mirror of https://github.com/odrling/Aegisub
Use agi::scoped_holder to close files in mkvwrap
This commit is contained in:
parent
bbd4cbef78
commit
9a21c13cbe
|
@ -44,6 +44,7 @@
|
||||||
#include "MatroskaParser.h"
|
#include "MatroskaParser.h"
|
||||||
|
|
||||||
#include <libaegisub/fs.h>
|
#include <libaegisub/fs.h>
|
||||||
|
#include <libaegisub/scoped_ptr.h>
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <boost/algorithm/string/classification.hpp>
|
#include <boost/algorithm/string/classification.hpp>
|
||||||
|
@ -135,19 +136,17 @@ static void read_subtitles(agi::ProgressSink *ps, MatroskaFile *file, MkvStdIO *
|
||||||
void MatroskaWrapper::GetSubtitles(agi::fs::path const& filename, AssFile *target) {
|
void MatroskaWrapper::GetSubtitles(agi::fs::path const& filename, AssFile *target) {
|
||||||
MkvStdIO input(filename);
|
MkvStdIO input(filename);
|
||||||
char err[2048];
|
char err[2048];
|
||||||
MatroskaFile *file = mkv_Open(&input, err, sizeof(err));
|
agi::scoped_holder<MatroskaFile*, decltype(&mkv_Close)> file(mkv_Open(&input, err, sizeof(err)), mkv_Close);
|
||||||
if (!file) throw MatroskaException(err);
|
if (!file) throw MatroskaException(err);
|
||||||
|
|
||||||
try {
|
|
||||||
// Get info
|
// Get info
|
||||||
unsigned tracks = mkv_GetNumTracks(file);
|
unsigned tracks = mkv_GetNumTracks(file);
|
||||||
std::vector<unsigned> tracksFound;
|
std::vector<unsigned> tracksFound;
|
||||||
std::vector<std::string> tracksNames;
|
std::vector<std::string> tracksNames;
|
||||||
unsigned trackToRead;
|
|
||||||
|
|
||||||
// Find tracks
|
// Find tracks
|
||||||
for (unsigned track = 0; track < tracks; track++) {
|
for (auto track : boost::irange(0u, tracks)) {
|
||||||
TrackInfo *trackInfo = mkv_GetTrackInfo(file, track);
|
auto trackInfo = mkv_GetTrackInfo(file, track);
|
||||||
if (trackInfo->Type != 0x11) continue;
|
if (trackInfo->Type != 0x11) continue;
|
||||||
|
|
||||||
// Known subtitle format
|
// Known subtitle format
|
||||||
|
@ -166,6 +165,7 @@ void MatroskaWrapper::GetSubtitles(agi::fs::path const& filename, AssFile *targe
|
||||||
if (tracksFound.empty())
|
if (tracksFound.empty())
|
||||||
throw MatroskaException("File has no recognised subtitle tracks.");
|
throw MatroskaException("File has no recognised subtitle tracks.");
|
||||||
|
|
||||||
|
unsigned trackToRead;
|
||||||
// Only one track found
|
// Only one track found
|
||||||
if (tracksFound.size() == 1)
|
if (tracksFound.size() == 1)
|
||||||
trackToRead = tracksFound[0];
|
trackToRead = tracksFound[0];
|
||||||
|
@ -180,7 +180,7 @@ void MatroskaWrapper::GetSubtitles(agi::fs::path const& filename, AssFile *targe
|
||||||
|
|
||||||
// Picked track
|
// Picked track
|
||||||
mkv_SetTrackMask(file, ~(1 << trackToRead));
|
mkv_SetTrackMask(file, ~(1 << trackToRead));
|
||||||
TrackInfo *trackInfo = mkv_GetTrackInfo(file, trackToRead);
|
auto trackInfo = mkv_GetTrackInfo(file, trackToRead);
|
||||||
std::string CodecID(trackInfo->CodecID);
|
std::string CodecID(trackInfo->CodecID);
|
||||||
bool srt = CodecID == "S_TEXT/UTF8";
|
bool srt = CodecID == "S_TEXT/UTF8";
|
||||||
bool ssa = CodecID == "S_TEXT/SSA";
|
bool ssa = CodecID == "S_TEXT/SSA";
|
||||||
|
@ -204,25 +204,20 @@ void MatroskaWrapper::GetSubtitles(agi::fs::path const& filename, AssFile *targe
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read timecode scale
|
// Read timecode scale
|
||||||
SegmentInfo *segInfo = mkv_GetFileInfo(file);
|
auto segInfo = mkv_GetFileInfo(file);
|
||||||
longlong timecodeScale = mkv_TruncFloat(trackInfo->TimecodeScale) * segInfo->TimecodeScale;
|
longlong timecodeScale = mkv_TruncFloat(trackInfo->TimecodeScale) * segInfo->TimecodeScale;
|
||||||
|
|
||||||
// Progress bar
|
// Progress bar
|
||||||
double totalTime = double(segInfo->Duration) / timecodeScale;
|
auto totalTime = double(segInfo->Duration) / timecodeScale;
|
||||||
DialogProgress progress(nullptr, _("Parsing Matroska"), _("Reading subtitles from Matroska file."));
|
DialogProgress progress(nullptr, _("Parsing Matroska"), _("Reading subtitles from Matroska file."));
|
||||||
progress.Run([&](agi::ProgressSink *ps) { read_subtitles(ps, file, &input, srt, totalTime, &parser); });
|
progress.Run([&](agi::ProgressSink *ps) { read_subtitles(ps, file, &input, srt, totalTime, &parser); });
|
||||||
}
|
|
||||||
catch (...) {
|
|
||||||
mkv_Close(file);
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool MatroskaWrapper::HasSubtitles(agi::fs::path const& filename) {
|
bool MatroskaWrapper::HasSubtitles(agi::fs::path const& filename) {
|
||||||
char err[2048];
|
char err[2048];
|
||||||
try {
|
try {
|
||||||
MkvStdIO input(filename);
|
MkvStdIO input(filename);
|
||||||
auto file = mkv_Open(&input, err, sizeof(err));
|
agi::scoped_holder<MatroskaFile*, decltype(&mkv_Close)> file(mkv_Open(&input, err, sizeof(err)), mkv_Close);
|
||||||
if (!file) return false;
|
if (!file) return false;
|
||||||
|
|
||||||
// Find tracks
|
// Find tracks
|
||||||
|
@ -232,20 +227,16 @@ bool MatroskaWrapper::HasSubtitles(agi::fs::path const& filename) {
|
||||||
|
|
||||||
if (trackInfo->Type == 0x11) {
|
if (trackInfo->Type == 0x11) {
|
||||||
std::string CodecID(trackInfo->CodecID);
|
std::string CodecID(trackInfo->CodecID);
|
||||||
if (CodecID == "S_TEXT/SSA" || CodecID == "S_TEXT/ASS" || CodecID == "S_TEXT/UTF8") {
|
if (CodecID == "S_TEXT/SSA" || CodecID == "S_TEXT/ASS" || CodecID == "S_TEXT/UTF8")
|
||||||
mkv_Close(file);
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mkv_Close(file);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
catch (...) {
|
catch (...) {
|
||||||
// We don't care about why we couldn't read subtitles here
|
// We don't care about why we couldn't read subtitles here
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
int StdIoRead(InputStream *_st, ulonglong pos, void *buffer, int count) {
|
int StdIoRead(InputStream *_st, ulonglong pos, void *buffer, int count) {
|
||||||
|
@ -274,9 +265,8 @@ longlong StdIoScan(InputStream *st, ulonglong start, unsigned signature) {
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
int c;
|
int c;
|
||||||
unsigned cmp = 0;
|
|
||||||
while ((c = getc(fp)) != EOF) {
|
while ((c = getc(fp)) != EOF) {
|
||||||
cmp = ((cmp << 8) | c) & 0xffffffff;
|
unsigned cmp = ((cmp << 8) | c) & 0xffffffff;
|
||||||
if (cmp == signature)
|
if (cmp == signature)
|
||||||
return std_ftell(fp) - 4;
|
return std_ftell(fp) - 4;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue