Use NSLinguisticTagger to deduce the language of the srt file. Patch by Aymeric Bard.
This commit is contained in:
1 parent
83f6b41d74
commit
6eaa5a11b3
2 files changed
+71
-16
No files matched your search
+28
-16
@@ -20,6 +20,8 @@
|
|||||||
#import "MP42PrivateUtilities.h"
|
#import "MP42PrivateUtilities.h"
|
||||||
#import "MP42Track+Muxer.h"
|
#import "MP42Track+Muxer.h"
|
||||||
|
|
||||||
|
#define SCALE_FACTOR 1000000.f
|
||||||
|
|
||||||
u_int32_t MP4AV_Ac3GetSamplingRate(u_int8_t* pHdr);
|
u_int32_t MP4AV_Ac3GetSamplingRate(u_int8_t* pHdr);
|
||||||
|
|
||||||
@interface MatroskaSample : NSObject {
|
@interface MatroskaSample : NSObject {
|
||||||
@@ -251,7 +253,7 @@ int readMkvPacket(struct StdIoStream *ioStream, TrackInfo *trackInfo, uint64_t
|
|||||||
|
|
||||||
double trackTimecodeScale = mkv_TruncFloat(mkvTrack->TimecodeScale);
|
double trackTimecodeScale = mkv_TruncFloat(mkvTrack->TimecodeScale);
|
||||||
SegmentInfo *segInfo = mkv_GetFileInfo(_matroskaFile);
|
SegmentInfo *segInfo = mkv_GetFileInfo(_matroskaFile);
|
||||||
UInt64 scaledDuration = (UInt64)segInfo->Duration / 1000000 * trackTimecodeScale;
|
UInt64 scaledDuration = (UInt64)segInfo->Duration / SCALE_FACTOR * trackTimecodeScale;
|
||||||
|
|
||||||
newTrack.duration = scaledDuration;
|
newTrack.duration = scaledDuration;
|
||||||
|
|
||||||
@@ -276,13 +278,13 @@ int readMkvPacket(struct StdIoStream *ioStream, TrackInfo *trackInfo, uint64_t
|
|||||||
MP42ChapterTrack *newTrack = [[MP42ChapterTrack alloc] init];
|
MP42ChapterTrack *newTrack = [[MP42ChapterTrack alloc] init];
|
||||||
|
|
||||||
SegmentInfo *segInfo = mkv_GetFileInfo(_matroskaFile);
|
SegmentInfo *segInfo = mkv_GetFileInfo(_matroskaFile);
|
||||||
UInt64 scaledDuration = (UInt64)segInfo->Duration / 1000000;
|
UInt64 scaledDuration = (UInt64)segInfo->Duration / SCALE_FACTOR;
|
||||||
[newTrack setDuration:scaledDuration];
|
[newTrack setDuration:scaledDuration];
|
||||||
|
|
||||||
if (count) {
|
if (count) {
|
||||||
unsigned int xi = 0;
|
unsigned int xi = 0;
|
||||||
for (xi = 0; xi < chapters->nChildren; xi++) {
|
for (xi = 0; xi < chapters->nChildren; xi++) {
|
||||||
uint64_t timestamp = (chapters->Children[xi].Start) / 1000000;
|
uint64_t timestamp = (chapters->Children[xi].Start) / SCALE_FACTOR;
|
||||||
if (!xi)
|
if (!xi)
|
||||||
timestamp = 0;
|
timestamp = 0;
|
||||||
if (xi && timestamp == 0)
|
if (xi && timestamp == 0)
|
||||||
@@ -316,7 +318,7 @@ int readMkvPacket(struct StdIoStream *ioStream, TrackInfo *trackInfo, uint64_t
|
|||||||
if (segInfo->Title)
|
if (segInfo->Title)
|
||||||
[mkvMetadata setTag:[NSString stringWithUTF8String:segInfo->Title] forKey:@"Name"];
|
[mkvMetadata setTag:[NSString stringWithUTF8String:segInfo->Title] forKey:@"Name"];
|
||||||
|
|
||||||
Tag* tags;
|
Tag *tags;
|
||||||
unsigned count;
|
unsigned count;
|
||||||
|
|
||||||
mkv_GetTags(_matroskaFile, &tags, &count);
|
mkv_GetTags(_matroskaFile, &tags, &count);
|
||||||
@@ -463,15 +465,15 @@ int readMkvPacket(struct StdIoStream *ioStream, TrackInfo *trackInfo, uint64_t
|
|||||||
mkv_ReadFrame(_matroskaFile, 0, &Track, &StartTime, &EndTime, &FilePos, &FrameSize, &FrameFlags);
|
mkv_ReadFrame(_matroskaFile, 0, &Track, &StartTime, &EndTime, &FilePos, &FrameSize, &FrameFlags);
|
||||||
mkv_Seek(_matroskaFile, 0, 0);
|
mkv_Seek(_matroskaFile, 0, 0);
|
||||||
|
|
||||||
return StartTime / 1000000;
|
return StartTime / SCALE_FACTOR;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (NSUInteger)timescaleForTrack:(MP42Track *)track
|
- (NSUInteger)timescaleForTrack:(MP42Track *)track
|
||||||
{
|
{
|
||||||
TrackInfo *trackInfo = mkv_GetTrackInfo(_matroskaFile, [track sourceId]);
|
TrackInfo *trackInfo = mkv_GetTrackInfo(_matroskaFile, [track sourceId]);
|
||||||
if (trackInfo->Type == TT_VIDEO)
|
if (trackInfo->Type == TT_VIDEO) {
|
||||||
return 100000;
|
return 100000;
|
||||||
else if (trackInfo->Type == TT_AUDIO) {
|
} else if (trackInfo->Type == TT_AUDIO) {
|
||||||
NSUInteger sampleRate = mkv_TruncFloat(trackInfo->AV.Audio.SamplingFreq);
|
NSUInteger sampleRate = mkv_TruncFloat(trackInfo->AV.Audio.SamplingFreq);
|
||||||
if (!strcmp(trackInfo->CodecID, "A_AC3")) {
|
if (!strcmp(trackInfo->CodecID, "A_AC3")) {
|
||||||
if (sampleRate < 24000)
|
if (sampleRate < 24000)
|
||||||
@@ -590,7 +592,7 @@ int readMkvPacket(struct StdIoStream *ioStream, TrackInfo *trackInfo, uint64_t
|
|||||||
|
|
||||||
MP42Track *track = nil;
|
MP42Track *track = nil;
|
||||||
MatroskaDemuxHelper *demuxHelper = nil;
|
MatroskaDemuxHelper *demuxHelper = nil;
|
||||||
MatroskaSample *frameSample = nil, * currentSample = nil;
|
MatroskaSample *frameSample = nil, *currentSample = nil;
|
||||||
int64_t offset, minOffset = 0, duration, next_duration;
|
int64_t offset, minOffset = 0, duration, next_duration;
|
||||||
|
|
||||||
const unsigned int bufferSize = 20;
|
const unsigned int bufferSize = 20;
|
||||||
@@ -627,6 +629,7 @@ int readMkvPacket(struct StdIoStream *ioStream, TrackInfo *trackInfo, uint64_t
|
|||||||
demuxHelper->samplesWritten++;
|
demuxHelper->samplesWritten++;
|
||||||
|
|
||||||
if (readMkvPacket(_ioStream, trackInfo, FilePos, &frame, &FrameSize)) {
|
if (readMkvPacket(_ioStream, trackInfo, FilePos, &frame, &FrameSize)) {
|
||||||
|
|
||||||
MP42SampleBuffer *sample = [[MP42SampleBuffer alloc] init];
|
MP42SampleBuffer *sample = [[MP42SampleBuffer alloc] init];
|
||||||
sample->data = frame;
|
sample->data = frame;
|
||||||
sample->size = FrameSize;
|
sample->size = FrameSize;
|
||||||
@@ -636,8 +639,17 @@ int readMkvPacket(struct StdIoStream *ioStream, TrackInfo *trackInfo, uint64_t
|
|||||||
sample->isSync = YES;
|
sample->isSync = YES;
|
||||||
sample->trackId = track.sourceId;
|
sample->trackId = track.sourceId;
|
||||||
|
|
||||||
[self enqueue:sample];
|
if (demuxHelper->previousSample) {
|
||||||
[sample release];
|
uint64_t duration = sample->timestamp / (double)1000000.f * (mkv_TruncFloat(trackInfo->AV.Audio.SamplingFreq) / 1000.f) - demuxHelper->current_time;
|
||||||
|
|
||||||
|
demuxHelper->previousSample->duration = duration;
|
||||||
|
[self enqueue:demuxHelper->previousSample];
|
||||||
|
|
||||||
|
demuxHelper->current_time += duration;
|
||||||
|
}
|
||||||
|
|
||||||
|
[demuxHelper->previousSample release];
|
||||||
|
demuxHelper->previousSample = sample;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -653,7 +665,7 @@ int readMkvPacket(struct StdIoStream *ioStream, TrackInfo *trackInfo, uint64_t
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ([string length]) {
|
if ([string length]) {
|
||||||
SBSubLine *sl = [[SBSubLine alloc] initWithLine:string start:StartTime/1000000 end:EndTime/1000000];
|
SBSubLine *sl = [[SBSubLine alloc] initWithLine:string start:StartTime / SCALE_FACTOR end:EndTime / SCALE_FACTOR];
|
||||||
[demuxHelper->ss addLine:[sl autorelease]];
|
[demuxHelper->ss addLine:[sl autorelease]];
|
||||||
}
|
}
|
||||||
demuxHelper->samplesWritten++;
|
demuxHelper->samplesWritten++;
|
||||||
@@ -674,7 +686,7 @@ int readMkvPacket(struct StdIoStream *ioStream, TrackInfo *trackInfo, uint64_t
|
|||||||
if (!strcmp(trackInfo->CodecID, "S_HDMV/PGS")) {
|
if (!strcmp(trackInfo->CodecID, "S_HDMV/PGS")) {
|
||||||
if (!demuxHelper->previousSample) {
|
if (!demuxHelper->previousSample) {
|
||||||
demuxHelper->previousSample = [[MP42SampleBuffer alloc] init];
|
demuxHelper->previousSample = [[MP42SampleBuffer alloc] init];
|
||||||
demuxHelper->previousSample->duration = StartTime / 1000000;
|
demuxHelper->previousSample->duration = StartTime / SCALE_FACTOR;
|
||||||
demuxHelper->previousSample->offset = 0;
|
demuxHelper->previousSample->offset = 0;
|
||||||
demuxHelper->previousSample->timestamp = 0;
|
demuxHelper->previousSample->timestamp = 0;
|
||||||
demuxHelper->previousSample->isSync = YES;
|
demuxHelper->previousSample->isSync = YES;
|
||||||
@@ -688,7 +700,7 @@ int readMkvPacket(struct StdIoStream *ioStream, TrackInfo *trackInfo, uint64_t
|
|||||||
demuxHelper->previousSample = temp;
|
demuxHelper->previousSample = temp;
|
||||||
}
|
}
|
||||||
|
|
||||||
demuxHelper->previousSample->duration = (nextSample->timestamp - demuxHelper->previousSample->timestamp) / 1000000;
|
demuxHelper->previousSample->duration = (nextSample->timestamp - demuxHelper->previousSample->timestamp) / SCALE_FACTOR;
|
||||||
}
|
}
|
||||||
|
|
||||||
[self enqueue:demuxHelper->previousSample];
|
[self enqueue:demuxHelper->previousSample];
|
||||||
@@ -701,7 +713,7 @@ int readMkvPacket(struct StdIoStream *ioStream, TrackInfo *trackInfo, uint64_t
|
|||||||
else if (!strcmp(trackInfo->CodecID, "S_VOBSUB")) {
|
else if (!strcmp(trackInfo->CodecID, "S_VOBSUB")) {
|
||||||
if (StartTime > demuxHelper->current_time) {
|
if (StartTime > demuxHelper->current_time) {
|
||||||
MP42SampleBuffer *sample = [[MP42SampleBuffer alloc] init];
|
MP42SampleBuffer *sample = [[MP42SampleBuffer alloc] init];
|
||||||
sample->duration = (StartTime - demuxHelper->current_time) / 1000000;
|
sample->duration = (StartTime - demuxHelper->current_time) / SCALE_FACTOR;
|
||||||
sample->size = 2;
|
sample->size = 2;
|
||||||
sample->data = calloc(1, 2);
|
sample->data = calloc(1, 2);
|
||||||
sample->isSync = YES;
|
sample->isSync = YES;
|
||||||
@@ -711,7 +723,7 @@ int readMkvPacket(struct StdIoStream *ioStream, TrackInfo *trackInfo, uint64_t
|
|||||||
[sample release];
|
[sample release];
|
||||||
}
|
}
|
||||||
|
|
||||||
nextSample->duration = (EndTime - StartTime ) / 1000000;
|
nextSample->duration = (EndTime - StartTime ) / SCALE_FACTOR;
|
||||||
|
|
||||||
[self enqueue:nextSample];
|
[self enqueue:nextSample];
|
||||||
[nextSample release];
|
[nextSample release];
|
||||||
@@ -742,7 +754,7 @@ int readMkvPacket(struct StdIoStream *ioStream, TrackInfo *trackInfo, uint64_t
|
|||||||
// matroska stores only the start and end time, so we need to recreate
|
// matroska stores only the start and end time, so we need to recreate
|
||||||
// the frame duration and the offset from the start time, the end time is useless
|
// the frame duration and the offset from the start time, the end time is useless
|
||||||
// duration calculation
|
// duration calculation
|
||||||
duration = ((MatroskaSample*)[demuxHelper->queue lastObject])->startTime - currentSample->startTime;
|
duration = ((MatroskaSample *)[demuxHelper->queue lastObject])->startTime - currentSample->startTime;
|
||||||
|
|
||||||
for (MatroskaSample *sample in demuxHelper->queue)
|
for (MatroskaSample *sample in demuxHelper->queue)
|
||||||
if (sample != currentSample && (sample->startTime >= currentSample->startTime))
|
if (sample != currentSample && (sample->startTime >= currentSample->startTime))
|
||||||
|
|||||||
@@ -36,6 +36,49 @@
|
|||||||
newTrack.alternate_group = 2;
|
newTrack.alternate_group = 2;
|
||||||
newTrack.language = getFilenameLanguage((CFStringRef)[_fileURL path]);
|
newTrack.language = getFilenameLanguage((CFStringRef)[_fileURL path]);
|
||||||
|
|
||||||
|
if ([newTrack.language isEqualToString:@"Unknown"]) {
|
||||||
|
// we couldn't deduce language from the fileURL
|
||||||
|
// -> Let's look into the file itself
|
||||||
|
|
||||||
|
NSString *stringFromFileAtURL = [[NSString alloc]
|
||||||
|
initWithContentsOfURL:fileURL
|
||||||
|
encoding:NSUTF8StringEncoding
|
||||||
|
error:nil];
|
||||||
|
if (stringFromFileAtURL) { // try auto determining
|
||||||
|
NSArray *tagschemes = [NSArray arrayWithObjects:NSLinguisticTagSchemeLanguage, nil];
|
||||||
|
NSLinguisticTagger *tagger = [[NSLinguisticTagger alloc] initWithTagSchemes:tagschemes options:0];
|
||||||
|
|
||||||
|
NSCountedSet *languagesSet = [NSCountedSet new];
|
||||||
|
|
||||||
|
[stringFromFileAtURL enumerateLinesUsingBlock:^(NSString *line, BOOL *stop) {
|
||||||
|
NSRange range = NSMakeRange(0, [line length]);
|
||||||
|
if (range.length > 1) {
|
||||||
|
[tagger setString:line];
|
||||||
|
/*NSString *language = */
|
||||||
|
[tagger tagAtIndex:0 scheme:NSLinguisticTagSchemeLanguage tokenRange:NULL sentenceRange:NULL];
|
||||||
|
NSOrthography *ortho = [tagger orthographyAtIndex:0 effectiveRange:NULL];
|
||||||
|
if (ortho && ![ortho.dominantLanguage isEqualToString:@"und"]) {
|
||||||
|
[languagesSet addObject:ortho.dominantLanguage];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}];
|
||||||
|
|
||||||
|
NSArray *sortedValues = [languagesSet.allObjects sortedArrayUsingComparator:^(id obj1, id obj2) {
|
||||||
|
NSUInteger n = [languagesSet countForObject:obj1];
|
||||||
|
NSUInteger m = [languagesSet countForObject:obj2];
|
||||||
|
return (n <= m)? (n < m)? NSOrderedAscending : NSOrderedSame : NSOrderedDescending;
|
||||||
|
}];
|
||||||
|
NSString *language = [sortedValues lastObject];
|
||||||
|
|
||||||
|
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en"];
|
||||||
|
NSString *languageName = [locale displayNameForKey:NSLocaleLanguageCode
|
||||||
|
value:language];
|
||||||
|
|
||||||
|
newTrack.language = languageName;
|
||||||
|
[locale release];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
_ss = [[SBSubSerializer alloc] init];
|
_ss = [[SBSubSerializer alloc] init];
|
||||||
if ([[_fileURL pathExtension] caseInsensitiveCompare: @"srt"] == NSOrderedSame) {
|
if ([[_fileURL pathExtension] caseInsensitiveCompare: @"srt"] == NSOrderedSame) {
|
||||||
success = LoadSRTFromPath([_fileURL path], _ss, &duration);
|
success = LoadSRTFromPath([_fileURL path], _ss, &duration);
|
||||||
|
|||||||
Reference in new issue
Block a user