如何从使用AVFoundation创建的相机上传视频

时间:2012-11-02 05:25:05

标签: iphone ios avfoundation

我创建了一个只录制视频并在服务器上发送的应用,我使用AVCam项目寻求帮助。并使用MKNetworkKit在服务器上上传视频。

但是当我在NSData中转换输出网址时,它会在我的NSLog中为我提供无限数据,我的xcode和iPhone都会挂起,所以请帮助。

这里我尝试过:

@implementation AVCamRecorder (FileOutputDelegate)

- (void)             captureOutput:(AVCaptureFileOutput *)captureOutput
didStartRecordingToOutputFileAtURL:(NSURL *)fileURL
                   fromConnections:(NSArray *)connections
{
                           //
    if ([[self delegate] respondsToSelector:@selector(recorderRecordingDidBegin:)]) {
        [[self delegate] recorderRecordingDidBegin:self];
    }

}

- (void)              captureOutput:(AVCaptureFileOutput *)captureOutput
didFinishRecordingToOutputFileAtURL:(NSURL *)anOutputFileURL
                    fromConnections:(NSArray *)connections
                              error:(NSError *)error
{

    videodata = [NSData dataWithContentsOfURL:outputFileURL];   //video data is my NSData

    NSLog(@"output video data is :%@",videodata);   //Here i have get non stop output data and hang
    NSLog(@"output file url is: %@",anOutputFileURL);


    if ([[self delegate] respondsToSelector:@selector(recorder:recordingDidFinishToOutputFileURL:error:)]) {
        [[self delegate] recorder:self recordingDidFinishToOutputFileURL:anOutputFileURL error:error];
    }

    //NSLog(@"captureOutput is: %@",captureOutput);



    //videoPath = [NSString stringWithContentsOfURL:anOutputFileURL encoding:NSUTF8StringEncoding error:nil];



    //videoPath = [anOutputFileURL absoluteString];

    //videoURL = anOutputFileURL;
   // videodata = captureOutput;
   // NSLog(@"video path is: %@",videodata);


    UIAlertView *message = [[UIAlertView alloc] initWithTitle:nil
                                                       message:@"Do you want to upload this content to the yes stream network ?"
                                                      delegate:self
                                             cancelButtonTitle:nil
                                             otherButtonTitles:@"Yes",@"No",nil];
    [message show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
    if([title isEqualToString:@"Yes"])
    {
        NSLog(@"Yes was selected.");



        self.flUploadEngine = [[fileUploadEngine alloc] initWithHostName:@"manektech.net" customHeaderFields:nil];

        NSMutableDictionary *postParams = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                           @"testApp", @"appID",
                                           nil];      
        self.flOperation = [self.flUploadEngine postDataToServer:postParams path:@"/dilipvideotest/savefile.php"];

        [self.flOperation addData:videodata forKey:@"uploadfile" mimeType:@"video/mov" fileName:@"output.mov" ];





        [self.flOperation onCompletion:^(MKNetworkOperation *operation) {

            NSLog(@"response string is : %@", [operation responseString]);


            /*   
             This is where you handle a successful 200 response
             */
        }     
                               onError:^(NSError *error) {
                                   NSLog(@"error : %@", error);
                                   UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                                                   message:[error localizedDescription]
                                                                                  delegate:nil
                                                                         cancelButtonTitle:@"Dismiss"
                                                                         otherButtonTitles:nil];
                                   [alert show];        
                               }];



        [self.flUploadEngine enqueueOperation:self.flOperation ];

    }
    else if([title isEqualToString:@"No"])
    {
        NSLog(@"No was selected.");
    }
}

@end

这是我的日志文件:

2012-11-02 11:16:25.585 AVFoundationCam[322:707] output file url is :file://localhost/private/var/mobile/Applications/4B2E02E5-3EE2-493E-8ECF-4B1DA29B9387/tmp/output.mov

2012-11-02 10:38:20.172 AVFoundationCam[157:707] output video data is :<00000014 66747970 71742020 00000000 71742020 00000008 77696465 0048f6a2 6d646174 00cc4007 010299cc 3dbd55cc 24c445ef bf3ae6f8 d24efffc 37c1ae35 2e5820d7 9111eef2 78c4b828 851be773 17f438d7 ed6ff47e afd5f439 cc3f2bed f60e0fb1 f2f6ba5e 81fcfbe2 df2b758e 373963b2 2b84f47e 0757c80a baf7abc8 cb0004a6 52001ff2 b257cbfb df66f0b6 3a325664 76dd35fa 758fa705 c1feeb4d 31376570 c4a967da 4d8d2fc2 d5b7c66b 30bc4477 9f0d0701 3099fe12 35a697bd 270d7556 3b24b833 7198a99a 314dcb28 32505930 9ac1a7a3 484c07a4 22d55921 115aae8f 481d2742 35c71af9 c94bd590 f841bf1d e2b9866b 10ecc5ba 333a9da7 9f1c5cb5 8f1a8686 619954c8 d4437dfb 4caa68bd 1164561c 2e15751a c29b0b41 34b1990e a6454c32 c472e79c 26d31699 95a6e164 e2c159ab 9a065e1f 1eaeec74 4445ddb1 11092ed0 b4f66d1a 68bb5a53 a1ee7eec a612b540 1a912034 07c7bffa 6733ad30 c8c43801 26d520c9 12220486 61153df9 ad6624a6 ab716849 25ca924b 912aab41 c16316eb 21a409d6 4175a0f6 9675e80f fbbe61ba 12cf4f7e af73a9a6 029961d7 0bed268a 97a116a0 e27add77 68de97c4 a476a450 9504cb0d d539f984 9b3af0f2 30c4632b AND STILL showing much more data and hang...............

1 个答案:

答案 0 :(得分:0)

不要尝试NSLog数据。您的视频文件可能非常大。 NSLog将以文本表示形式打印出数据的每个字节。如果你需要知道你的电影有多大,你可以打印出字节数:

NSLog(@"output video data size is:%d", videodata.length);

我不熟悉MKNetworkOperation,您需要弄清楚是否有任何数据大小限制。 此外,电影文件通常都很大。因此,在分配大量数据块时,您的应用程序可能会耗尽内存。如果服务器端能够以这种方式接收电影,我会考虑通过流逐段发送电影。