IOS Note-SRT Video subtitle parsing and synchronization

Source: Internet
Author: User

Format of SRT subtitles

100:00:31,788---00:00:34,020{\a3} I'll share fourth question for you 200:00:34,412----00:00:35,740{\a3} How to start a business 300:00:36,140-- 00:00:37,684{\A3} How to start a business 400:00:37,964--and 00:00:39,060{\A3} in this problem 500:00:39,268--00:00:41,692{\a3} I'm going to tell you two things 600:00:41,892----00:00:43,868{\a3} The first aspect is the process of starting a business 700:00:44,260--and 00:00:45,500{\A3} The second aspect of the content
This is my video subtitle.
First parse out the video subtitle time and subtitle content.
NSString *appendstr =@"HTTP://192.168.1.60:5080/MOOC/CAPTION/DC05F541-269C-4F1B-AD99-782F5ADCEFDF_1447910856426.SRT"; Nsurlrequest*request =[Nsurlrequest Requestwithurl:[nsurl urlwithstring:appendstr]; Nsurlsession*session =[Nsurlsession sharedsession]; Nsurlsessiontask*datatask = [Session datataskwithrequest:request completionhandler:^ (NSData *data, Nsurlresponse *response, NSError *error) {        if(!error) {            //GBK EncodingNsstringencoding enc =cfstringconvertencodingtonsstringencoding (kcfstringencodinggb_18030_2000); //decodingNSString *string=[[NSString alloc] Initwithdata:data Encoding:enc]; //split into arrays by rowNsarray *singlearray = [stringComponentsseparatedbystring:@"\ n"]; _subtitlesarray=[Nsmutablearray array]; _begintimearray=[Nsmutablearray array]; _endtimearray=[Nsmutablearray array]; //Traverse an array that holds all rows             for(NSString *ssinchSinglearray) {Nsrange Range= [SS Rangeofstring:@"{\\A3}"]; Nsrange Range2= [SS Rangeofstring:@" -"]; if(Range.location! =nsnotfound) {[_subtitlesarray addobject:[ss substringFromIndex:range.location+Range.length]]; }                if(Range2.location! =nsnotfound) {NSString*beginstr =[SS SubstringToIndex:range2.location]; NSString*ENDSTR = [SS 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];                                        [_begintimearray 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];                [_endtimearray Addobject:endnum]; }} NSLog (@"start time Array-=-=-==-=%@", _begintimearray); NSLog (@"end time Array-=-=-==-=%@", _endtimearray); NSLog (@"subtitle Array-=-=-==-=%@", _subtitlesarray); }Else{NSLog (@"error is%@", error.localizeddescription);    }    }]; [Datatask resume];

The above code has put the start time, the end time, the subtitles are placed in three arrays respectively.

Change the time in seconds, in order to be able to match the current time obtained and then compare.

Video playback with a timer, monitoring the current time, with the start time, the end time comparison, in between the corresponding caption put in the display is right

Self.timer = [Nstimer scheduledtimerwithtimeinterval:0.05 target:self selector: @selector (timeaction) Userinfo:nil Repeats:yes];
- (void) timeaction{//Judging playback status    if(_player.rate! =1) {        return; } Nsinteger Currentsecond=cmtimegetseconds (_player.currentitem.currenttime);
for(inti =0; I<_begintimearray.count; i++) {Nsinteger Beginarr=[_begintimearray[i] integervalue]; Nsinteger Endarr=[_endtimearray[i]integervalue]; if(Currentsecond > Beginarr && currentsecond<Endarr) { //Sync Subtitles_subtitleslabel.text =_subtitlesarray[i]; NSLog (@"subtitle%@", _subtitlesarray[i]); } } //Play Complete Pause if(Currentsecond = = (int) Cmtimegetseconds (self.player.currentItem.duration)) {[Self.player pause]; [Self UpdateUI]; }}

Note here that the time is converted to Nsinteger type comparison, the first use of the NSNumber comparison, subtitles display confusion =. =

So the subtitles show out, if the subtitles and video dialogue is biased, the timing of the timer adjustment of a little bit should be good ~

IOS Note-SRT Video subtitle parsing and synchronization

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.