| FFmpegKit iOS / macOS / tvOS API 6.0

Loading...
Searching...
No Matches
MediaInformationJsonParser.m
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018-2022 Taner Sener
3 *
4 * This file is part of FFmpegKit.
5 *
6 * FFmpegKit is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * FFmpegKit is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with FFmpegKit. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#import "MediaInformationJsonParser.h"
21
22NSString* const MediaInformationJsonParserKeyStreams = @"streams";
23NSString* const MediaInformationJsonParserKeyChapters = @"chapters";
24
25@implementation MediaInformationJsonParser
26
27+ (MediaInformation*)from:(NSString*)ffprobeJsonOutput {
28 @try {
29 return [self fromWithError:ffprobeJsonOutput];
30 } @catch (NSException *exception) {
31 NSLog(@"MediaInformation parsing failed: %@.\n", [NSString stringWithFormat:@"%@\n%@", [exception userInfo], [exception callStackSymbols]]);
32 return nil;
33 }
34}
35
36+ (MediaInformation*)fromWithError:(NSString*)ffprobeJsonOutput {
37 NSError* error = nil;
38 NSData *jsonData = [ffprobeJsonOutput dataUsingEncoding:NSUTF8StringEncoding];
39 NSDictionary *jsonDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&error];
40 if (error != nil) {
41 @throw [NSException exceptionWithName:@"ParsingException" reason:[NSString stringWithFormat:@"%ld",(long)[error code]] userInfo:[error userInfo]];
42 }
43 if (jsonDictionary == nil) {
44 return nil;
45 }
46
47 NSArray* jsonStreamArray = [jsonDictionary objectForKey:MediaInformationJsonParserKeyStreams];
48 NSMutableArray *streamArray = [[NSMutableArray alloc] init];
49 for(int i = 0; i<jsonStreamArray.count; i++) {
50 NSDictionary *streamDictionary = [jsonStreamArray objectAtIndex:i];
51 [streamArray addObject:[[StreamInformation alloc] init:streamDictionary]];
52 }
53
54 NSArray* jsonChapterArray = [jsonDictionary objectForKey:MediaInformationJsonParserKeyChapters];
55 NSMutableArray *chapterArray = [[NSMutableArray alloc] init];
56 for(int i = 0; i<jsonChapterArray.count; i++) {
57 NSDictionary *chapterDictionary = [jsonChapterArray objectAtIndex:i];
58 [chapterArray addObject:[[Chapter alloc] init:chapterDictionary]];
59 }
60
61 return [[MediaInformation alloc] init:jsonDictionary withStreams:streamArray withChapters:chapterArray];
62}
63
64@end
chapterArray
NSArray * chapterArray
Definition MediaInformation.m:47
streamArray
NSArray * streamArray
Definition MediaInformation.m:42
MediaInformationJsonParser.h
MediaInformationJsonParserKeyChapters
NSString *const MediaInformationJsonParserKeyChapters
Definition MediaInformationJsonParser.m:23
MediaInformationJsonParserKeyStreams
NSString *const MediaInformationJsonParserKeyStreams
Definition MediaInformationJsonParser.m:22
Chapter
Definition Chapter.h:37
MediaInformation
Definition MediaInformation.h:41
MediaInformationJsonParser
Definition MediaInformationJsonParser.h:30
+[MediaInformationJsonParser fromWithError:]
MediaInformation * fromWithError:(NSString *ffprobeJsonOutput)
Definition MediaInformationJsonParser.m:36
StreamInformation
Definition StreamInformation.h:48