Problem Description: Video playback requires matching subtitles
The idea is to first resolve the subtitle (Chinese and English) and then synchronize subtitles (in the Player time Update method to add the corresponding time caption)
First, about the SRT file
1.1 Open Mode
In the SRT file, right-click the "Other"--application---text editor. App, using a text editor to view
1.2 Format description
1xx:xx:Geneva,960-xx:xx: the,260"60 Seconds of economic exploration" Sixth: rational choice theory -Second AdventuresinchEconomics--Number six:rational Choice theory2xx:xx: the,290-xx:xx: -,950One of the most frustrating things about running an economy is the things to factorinchWhen running a economy, the most troublesome ispeople.3xx:xx: -,950-xx:xx: -,460in general, man is rational now by and large--humans is a rational lot.4xx:xx: -,460-xx:xx: -, theThe price increases when people will supply more to buy less when the prices of something rises people will supply more it--and buy less it.5xx:xx: -, the-xx:xx: -, theIf inflation rises people will ask for higher wages if they expect inflation to go up--people would usually ask forHigher wages--6xx:xx: -, the-xx:xx: -,660just might not get it (though they might notGetthem)7xx:xx: -,660-xx:xx: -,660to see a country's interest rate or exchange rate fall when andifThey can see interest or exchange rates fallinginchOne country,
In general, the SRT file format is very fixed, the above format can be seen as
Digital
Start time-end time
Subtitle content (can be multiple lines)
Blank Line
This fixed style, each five lines of a complete subtitle line;
1.3 Parsing methods
#pragmaMark-Subtitles-(void) getvideosubtitles{//nsstring *pathstr = [[NSBundle mainbundle] pathforresource:@ "VideoSubtitles1" oftype:@ "SRT"];//nsurlrequest *request = [nsurlrequest requestwithurl:[nsurl fileurlwithpath:pathstr];NSString *pathstr =[Self.subtitlepath Stringbyaddingpercentencodingwithallowedcharacters:[nscharacterset Urlqueryallowedcharacterset]]; Nsurlrequest*request =[Nsurlrequest Requestwithurl:[nsurl urlwithstring:pathstr]; Nsurlsession*session =[Nsurlsession sharedsession]; Nsurlsessiondatatask*datatask = [Session datataskwithrequest:request completionhandler:^ (NSData * _nullable data, Nsurlresponse * _Nullable R Esponse, Nserror *_nullable Error) { if(!error) {NSString*string=[[Nsmutablestring alloc] Initwithdata:data encoding:nsutf8stringencoding]; //split into arrays by rowNsarray *singlearray = [stringComponentsseparatedbystring:@"\ n"]; Nsmutablearray*begintimearray1 =[Nsmutablearray array]; Nsmutablearray*endtimearray1 =[Nsmutablearray array]; Nsmutablearray* Subtitlesarray1 =[Nsmutablearray array]; NSString*substr =@""; for(inti =0; i < Singlearray.count; i++) { if((i%5) ==0) { }Else if((i%5) ==1) { //TimeNSString *timestr =[Singlearray objectatindex:i]; Nsrange Range2= [Timestr rangeofstring:@" -"]; if(Range2.location! =nsnotfound) {NSString*beginstr =[Timestr substringToIndex:range2.location]; NSString*ENDSTR = [Timestr substringfromindex:range2.location+Range2.length]; Nsarray* arr = [Beginstr componentsseparatedbystring:@":"]; Nsarray* arr1 = [arr[2] Componentsseparatedbystring:@","]; //converts the time in the start time array into seconds floatteim=[arr[0] Floatvalue] * -* -+ [arr[1] Floatvalue]* -+ [arr1[0] Floatvalue] + [arr1[1] floatvalue]/ +; //Convert float type to NSNumber type to deposit arrayNSNumber *beginnum =[NSNumber Numberwithfloat:teim]; [Begintimearray1 Addobject:beginnum]; Nsarray* array = [ENDSTR componentsseparatedbystring:@":"]; Nsarray* ARR2 = [array[2] Componentsseparatedbystring:@","]; //converts the time in an end-time array into seconds floatfl=[array[0] Floatvalue] * -* -+ [array[1] Floatvalue]* -+ [arr2[0] Floatvalue] + [arr2[1] floatvalue]/ +; NSNumber*endnum =[NSNumber NUMBERWITHFLOAT:FL]; [Endtimearray1 Addobject:endnum]; } }Else { if((i%5) ==2) { //Chinese subtitlesSUBSTR = [NSString stringWithFormat:@"%@", [Singlearray objectatindex:i]]; }Else if((i%5) ==3) { //English OriginalSUBSTR = [SubStr stringbyappendingformat:@"\n%@", [Singlearray objectatindex:i]]; [Subtitlesarray1 ADDOBJECT:SUBSTR]; SubStr=@""; }}} Dispatch_async (Dispatch_get_main_queue (),^{_begintimesubarr=Begintimearray1; _endtimesubarr=Endtimearray1; _subtitlearr=Subtitlesarray1; }); NSLog (@"start time Array-=-=-==-=%@", Begintimearray1); NSLog (@"end time Array-=-=-==-=%@", Endtimearray1); NSLog (@"subtitle Array-=-=-==-=%@", Subtitlesarray1); }Else{NSLog (@"error is%@", error.localizeddescription); } }]; [Datatask resume]; }
What needs to get here is a subtitle of the start time, end time and subtitle text three content, respectively deposited in three arrays, the time units converted into seconds.
1.4 Synchronization methods
#pragmamark-Update Method-(void) update{//NSLog (@ "---timer method---");_labcurrenttime.text =[self TimeformatFromSeconds:self.player.currentPlaybackTime]; CGFloat Current=Self.player.currentPlaybackTime; CGFloat Total=self.player.duration; CGFloat Able=self.player.playableDuration; [_slider setvalue:current/Total Animated:yes]; [_progressview setprogress:able/Total Animated:yes];//NSLog (@ "Print total length:%.2f", self.player.duration);self.videostudypecent= (current/total) * -;//NSLog (@ "Video--_begintimesubarr:%ld", _begintimesubarr.count); //Subtitle SynchronizationNsinteger Currentsecond =Self.player.currentPlaybackTime; for(inti =0; I<_begintimesubarr.count; i++) {Nsinteger Beginarr=[_begintimesubarr[i] integervalue]; Nsinteger Endarr=[_endtimesubarr[i]integervalue]; if(Currentsecond > Beginarr && currentsecond<Endarr) { //Sync Subtitles_subtitlelab.text =_subtitlearr[i];//NSLog (@ "subtitle%@", _subtitlearr[i]); } } //NSLog (@ "Print video learning ratio%ld", self.videostudypecent);}
In the player's time Update method, iterate through the subtitle start time array to update the caption text based on whether the current time is at the beginning of a marquee and the end time period. If the caption text appears to differ from the sound, you can try to adjust the time accuracy of the caption corresponding to the resolution.
Second, supplementary
2.1 SRT File
uploaded a sample video and a companion SRT file to Csdn, which can be downloaded to test the sound and text synchronization
: http://download.csdn.net/download/wusangtongxue/10106389
2.2 Reference URL:
http://www.cnblogs.com/ruixin2222/p/5040831.html (main reference blog)
http://blog.csdn.net/hhbgk/article/details/22435723 (Android caption SRT parsing instructions)
IOS Video Subtitle srt file parsing