objective-c – 在iPhone上的Objective C中将视频上传到Vimeo
栏目: Objective-C · 发布时间: 5年前
内容简介:我正在开发一个应用程序,我想将视频上传到Vimeo,Facebook和Youtube. Facebook和Youtube有非常简单的apis,Vimeo有很好的开发人员文档,但没有Objective C框架.我见过几个使用Vimeo的应用程序,所以我想知道是否有某种框架,我不知道.翻译自:https://stackoverflow.com/questions/7583573/upload-video-to-vimeo-in-objective-c-on-iphone
我正在开发一个应用程序,我想将视频上传到Vimeo,Facebook和Youtube. Facebook和Youtube有非常简单的apis,Vimeo有很好的开发人员文档,但没有Objective C框架.我见过几个使用Vimeo的应用程序,所以我想知道是否有某种框架,我不知道.
eo感兴趣,请参阅以下代码.首先,您需要使用vimeo注册应用程序并获取您的密钥和消费者密钥.然后你需要从谷歌和可能的SBJson框架获得GTMOAuth框架.目前,我很遗憾没有时间清理下面的代码,但我认为对于那些需要vimeo帮助的人来说,这可能比什么都好.基本上,您使用vimeo进行身份验证,获取上传票证,使用此票证上传视频,然后添加标题和一些文本.
下面的代码不会开箱即用,因为有几个视图元素已连接,但它应该让您了解正在发生的事情.
#define VIMEO_SECRET @"1234567890" #define VIMEO_CONSUMER_KEY @"1234567890" #define VIMEO_BASE_URL @"http://vimeo.com/services/auth/" #define VIMEO_REQUEST_TOKEN_URL @"http://vimeo.com/oauth/request_token" #define VIMEO_AUTHORIZATION_URL @"http://vimeo.com/oauth/authorize?permission=write" #define VIMEO_ACCESS_TOKEN_URL @"http://vimeo.com/oauth/access_token" #import "MMVimeoUploaderVC.h" #import "GTMOAuthAuthentication.h" #import "GTMOAuthSignIn.h" #import "GTMOAuthViewControllerTouch.h" #import "JSON.h" @interface MMVimeoUploaderVC () @property (retain) GTMOAuthAuthentication *signedAuth; @property (retain) NSString *currentTicketID; @property (retain) NSString *currentVideoID; @property (assign) BOOL isUploading; @property (retain) GTMHTTPFetcher *currentFetcher; @end @implementation MMVimeoUploaderVC - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization first = YES; [GTMOAuthViewControllerTouch removeParamsFromKeychainForName:@"Vimeo"]; } return self; } - (void)stopUpload { if ( self.isUploading || self.currentFetcher ) { [self.currentFetcher stopFetching]; } } - (void) setProgress:(float) progress { // Connect to your views here } #pragma mark - handle error - (void) handleErrorWithText:(NSString *) text { //notify your views here self.currentFetcher = nil; self.isUploading = NO; self.progressBar.alpha = 0; self.uploadButton.alpha = 1; } #pragma mark - interface callbacks //step one, authorize - (void)startUpload { if ( self.signedAuth ) { //authentication present, start upload } else { //get vimeo authentication NSURL *requestURL = [NSURL URLWithString:VIMEO_REQUEST_TOKEN_URL]; NSURL *accessURL = [NSURL URLWithString:VIMEO_ACCESS_TOKEN_URL]; NSURL *authorizeURL = [NSURL URLWithString:VIMEO_AUTHORIZATION_URL]; NSString *scope = @""; GTMOAuthAuthentication *auth = [self vimeoAuth]; // set the callback URL to which the site should redirect, and for which // the OAuth controller should look to determine when sign-in has // finished or been canceled // // This URL does not need to be for an actual web page [auth setCallback:@"http://www.....com/OAuthCallback"]; // Display the autentication view GTMOAuthViewControllerTouch *viewController; viewController = [[[GTMOAuthViewControllerTouch alloc] initWithScope:scope language:nil requestTokenURL:requestURL authorizeTokenURL:authorizeURL accessTokenURL:accessURL authentication:auth appServiceName:@"Vimeo" delegate:self finishedSelector:@selector(viewController:finishedWithAuth:error:)] autorelease]; [[self navigationController] pushViewController:viewController animated:YES]; } } //step two get upload ticket - (void)viewController:(GTMOAuthViewControllerTouch *)viewController finishedWithAuth:(GTMOAuthAuthentication *)auth error:(NSError *)error { if (error != nil) { [self handleErrorWithText:nil]; } else { self.signedAuth = auth; [self startUpload]; } } - (void) startUpload { self.isUploading = YES; NSURL *url = [NSURL URLWithString:@"http://vimeo.com/api/rest/v2?format=json&method=vimeo.videos.upload.getQuota"]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; [self.signedAuth authorizeRequest:request]; GTMHTTPFetcher* myFetcher = [GTMHTTPFetcher fetcherWithRequest:request]; [myFetcher beginFetchWithDelegate:self didFinishSelector:@selector(myFetcher:finishedWithData:error:)]; self.currentFetcher = myFetcher; } - (void) myFetcher:(GTMHTTPFetcher *)fetcher finishedWithData:(NSData *)data error:(NSError *) error { if (error != nil) { [self handleErrorWithText:nil]; NSLog(@"error %@", error); } else { NSString *info = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; NSDictionary *result = [info JSONValue]; //quota int quota = [[result valueForKeyPath:@"user.upload_space.max"] intValue]; //get video file size NSString *path; path = @"local video path"; NSFileManager *manager = [NSFileManager defaultManager]; NSDictionary *attrs = [manager attributesOfItemAtPath:path error: NULL]; UInt32 size = [attrs fileSize]; if ( size > quota ) { [self handleErrorWithText:@"Your Vimeo account quota is exceeded."]; return; } //lets assume we have enough quota NSURL *url = [NSURL URLWithString:@"http://vimeo.com/api/rest/v2?format=json&method=vimeo.videos.upload.getTicket&upload_method=streaming"]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; [self.signedAuth authorizeRequest:request]; GTMHTTPFetcher* myFetcher = [GTMHTTPFetcher fetcherWithRequest:request]; [myFetcher beginFetchWithDelegate:self didFinishSelector:@selector(myFetcher2:finishedWithData:error:)]; self.currentFetcher = myFetcher; } } - (void) myFetcher2:(GTMHTTPFetcher *)fetcher finishedWithData:(NSData *)data error:(NSError *) error { if (error != nil) { [self handleErrorWithText:nil]; NSLog(@"error %@", error); } else { NSString *info = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; NSDictionary *result = [info JSONValue]; //fail here if neccessary TODO NSString *urlString = [result valueForKeyPath:@"ticket.endpoint"]; self.currentTicketID = [result valueForKeyPath:@"ticket.id"]; if ( [self.currentTicketID length] == 0 || [urlString length] == 0) { [self handleErrorWithText:nil]; return; } //get video file // load the file data NSString *path; path = [MMMovieRenderer sharedRenderer].localVideoURL; //get video file size NSFileManager *manager = [NSFileManager defaultManager]; NSDictionary *attrs = [manager attributesOfItemAtPath:path error: NULL]; UInt32 size = [attrs fileSize]; //insert endpoint here NSURL *url = [NSURL URLWithString:urlString]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; [request setHTTPMethod:@"PUT"]; [request setValue:[NSString stringWithFormat:@"%i", size] forHTTPHeaderField:@"Content-Length"]; [request setValue:@"video/mp4" forHTTPHeaderField:@"Content-Type"]; [request setHTTPBody:[NSData dataWithContentsOfFile:path]]; [self.signedAuth authorizeRequest:request]; GTMHTTPFetcher* myFetcher = [GTMHTTPFetcher fetcherWithRequest:request]; myFetcher.sentDataSelector = @selector(myFetcher:didSendBytes:totalBytesSent:totalBytesExpectedToSend:); [myFetcher beginFetchWithDelegate:self didFinishSelector:@selector(myFetcher3:finishedWithData:error:)]; self.currentFetcher = myFetcher; } } - (void)myFetcher:(GTMHTTPFetcher *)fetcher didSendBytes:(NSInteger)bytesSent totalBytesSent:(NSInteger)totalBytesSent totalBytesExpectedToSend:(NSInteger)totalBytesExpectedToSend { NSLog(@"%i / %i", totalBytesSent, totalBytesExpectedToSend); [self setProgress:(float)totalBytesSent / (float) totalBytesExpectedToSend]; self.uploadLabel.text = @"Uploading to Vimeo..."; } - (void) myFetcher3:(GTMHTTPFetcher *)fetcher finishedWithData:(NSData *)data error:(NSError *) error { if (error != nil) { [self handleErrorWithText:nil]; } else { NSString *info = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; //finalize upload NSString *requestString = [NSString stringWithFormat:@"http://vimeo.com/api/rest/v2?format=json&method=vimeo.videos.upload.complete&ticket_id=%@&filename=%@", self.currentTicketID, @"movie.mov"]; NSURL *url = [NSURL URLWithString:requestString]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; [self.signedAuth authorizeRequest:request]; GTMHTTPFetcher* myFetcher = [GTMHTTPFetcher fetcherWithRequest:request]; [myFetcher beginFetchWithDelegate:self didFinishSelector:@selector(myFetcher4:finishedWithData:error:)]; self.currentFetcher = myFetcher; } } - (void) myFetcher4:(GTMHTTPFetcher *)fetcher finishedWithData:(NSData *)data error:(NSError *) error { if (error != nil) { [self handleErrorWithText:nil]; } else { //finish upload NSString *info = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; NSDictionary *result = [info JSONValue]; self.currentVideoID = [result valueForKeyPath:@"ticket.video_id"]; if ( [self.currentVideoID length] == 0 ) { [self handleErrorWithText:nil]; return; } //set title NSString *title = [MMMovieSettingsManager sharedManager].movieTitle; title = [title stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]; NSString *requestString = [NSString stringWithFormat:@"http://vimeo.com/api/rest/v2?format=json&method=vimeo.videos.setTitle&video_id=%@&title=%@", self.currentVideoID, title]; NSLog(@"%@", requestString); NSURL *url = [NSURL URLWithString:requestString]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; [self.signedAuth authorizeRequest:request]; GTMHTTPFetcher* myFetcher = [GTMHTTPFetcher fetcherWithRequest:request]; [myFetcher beginFetchWithDelegate:self didFinishSelector:@selector(myFetcher5:finishedWithData:error:)]; } } - (void) myFetcher5:(GTMHTTPFetcher *)fetcher finishedWithData:(NSData *)data error:(NSError *) error { if (error != nil) { [self handleErrorWithText:nil]; NSLog(@"error %@", error); } else { //set description NSString *desc = @"Video created with ... iPhone App."; desc = [desc stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]; NSString *requestString = [NSString stringWithFormat:@"http://vimeo.com/api/rest/v2?format=json&method=vimeo.videos.setDescription&video_id=%@&description=%@", self.currentVideoID, desc]; NSURL *url = [NSURL URLWithString:requestString]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; [self.signedAuth authorizeRequest:request]; GTMHTTPFetcher* myFetcher = [GTMHTTPFetcher fetcherWithRequest:request]; [myFetcher beginFetchWithDelegate:self didFinishSelector:@selector(myFetcher6:finishedWithData:error:)]; } } - (void) myFetcher6:(GTMHTTPFetcher *)fetcher finishedWithData:(NSData *)data error:(NSError *) error { if (error != nil) { [self handleErrorWithText:nil]; NSLog(@"error %@", error); } else { //done //alert your views that the upload has been completed } } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. [self setProgress:0]; } - (void) viewDidDisappear:(BOOL)animated { [super viewDidDisappear:animated]; [GTMOAuthViewControllerTouch removeParamsFromKeychainForName:@"Vimeo"]; } #pragma mark - oauth stuff - (GTMOAuthAuthentication *)vimeoAuth { NSString *myConsumerKey = VIMEO_CONSUMER_KEY; // pre-registered with service NSString *myConsumerSecret = VIMEO_SECRET; // pre-assigned by service GTMOAuthAuthentication *auth; auth = [[[GTMOAuthAuthentication alloc] initWithSignatureMethod:kGTMOAuthSignatureMethodHMAC_SHA1 consumerKey:myConsumerKey privateKey:myConsumerSecret] autorelease]; // setting the service name lets us inspect the auth object later to know // what service it is for auth.serviceProvider = @"Vimeo"; return auth; } @end
翻译自:https://stackoverflow.com/questions/7583573/upload-video-to-vimeo-in-objective-c-on-iphone
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- python中将list转为dict
- Spring中将header头转换为参数
- 数组 – 如何在Swift中将数组拆分成两半?
- postgresql – 在postgres中将表列名更改为大写
- 如何在Java 8中将List转换为Map?
- 如何在Kubernetes中将Envoy用作负载均衡器
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
深度探索Linux操作系统
王柏生 / 机械工业出版社 / 2013-10-15 / 89.00
《深度探索linux操作系统:系统构建和原理解析》是探索linux操作系统原理的里程碑之作,在众多的同类书中独树一帜。它颠覆和摒弃了传统的从阅读linux内核源代码着手学习linux操作系统原理的方式,而是基于实践,以从零开始构建一个完整的linux操作系统的过程为依托,指引读者在实践中去探索操作系统的本质。这种方式的妙处在于,让读者先从宏观上全面认清一个完整的操作系统中都包含哪些组件,各个组件的......一起来看看 《深度探索Linux操作系统》 这本书的介绍吧!
SHA 加密
SHA 加密工具
RGB CMYK 转换工具
RGB CMYK 互转工具