Monday, August 20, 2018

how can I open a video from dailymotion with mpmovieplayercontroller

I want to open a video from dailymotion with mpmovieplayercontroller. I have tried Dailymotion SDK but it seems that it just embeds a video to a uiwebview. I wonder if there is a dailymotion parser to get the video link just like hcyoutubeparser or ytvvimeoextractor.

Solved

-(NSDictionary*)getInfoForDailyMotionVideo:(NSString*)videoId
{
NSString *urlString = [NSString stringWithFormat:@"http://www.dailymotion.com/embed/video/%@", videoId];
NSURL *URL = [NSURL URLWithString:urlString];
NSData * data = [[NSData alloc] initWithContentsOfURL:URL];
NSString *html = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSString *str;

NSRange startRange = [html rangeOfString:@"var info = {"];
str = [html substringFromIndex:startRange.location];
NSRange endRange = [str rangeOfString:@"{"];
str = [str substringFromIndex:endRange.location];
endRange = [str rangeOfString:@"},"];
NSString *jsonString = [str substringToIndex:endRange.location+1];


NSDictionary *result = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
return result;
}

Passing a video ID of xct647, will return a dictionary with the following keys that work with a MPMoviePlayerViewController:

"stream_h264_hq_url" = "http://www.dailymotion.com/cdn/H264-848x480/video/xct647.mp4?auth=1371275189-0d67b3d0c242a8b439adf4300014d749";
"stream_h264_ld_url" = "http://www.dailymotion.com/cdn/H264-320x240/video/xct647.mp4?auth=1371275189-f581218251d42809bf13ad96ea6aacb8";
"stream_h264_url" = "http://www.dailymotion.com/cdn/H264-512x384/video/xct647.mp4?auth=1371275189-f905cebbbe96c563beca38ab59132a95";
"stream_hls_url" = "http://www.dailymotion.com/cdn/manifest/video/xct647.m3u8?auth=1371275189-dbdb650a58afe8661ec6aa9628de064f";

Use MPMovieSourceTypeFile for the h264 url's and MPMovieSourceTypeStreaming for the 'hls' url.


the last answer is a great starting point, but doesn't work anymore, needed to update due to Javascript/JSON Syntax changes:

-(NSDictionary*)getInfoForDailyMotionVideo:(NSString*)videoId{
    NSString *urlString = [NSString stringWithFormat:@"http://www.dailymotion.com/embed/video/%@", videoId];
    NSURL *URL = [NSURL URLWithString:urlString];
    NSData * data = [[NSData alloc] initWithContentsOfURL:URL];
    NSString *html = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSString *str;

    NSRange startRange = [html rangeOfString:@"var config = {"];
    str = [html substringFromIndex:startRange.location];
    NSRange endRange = [str rangeOfString:@"{"];
    str = [str substringFromIndex:endRange.location];
    endRange = [str rangeOfString:@"}};"];
    NSString *jsonString = [str substringToIndex:endRange.location+1];
    jsonString = [jsonString stringByAppendingString:@"}"];
    NSError *jsonError;
    NSDictionary *result = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding] options:0 error:&jsonError];
    return result;
}

This now works and returns all relevant values for AVPlayer etc...

Have fun!


No comments:

Post a Comment