2012-10-03 15 views
11

वर्तमान में मैं एक ऐसे एप्लिकेशन पर काम कर रहा हूं जो वीडियो से संबंधित है। मेरे आवेदन में उपयोगकर्ता वीडियो को ट्रिम कर सकता है, मेरे पास प्रारंभ समय और समाप्ति समय चुनने के लिए कस्टम नियंत्रण है। मुझे इन दो मूल्यों से वीडियो को ट्रिम करने की ज़रूरत है। मैंने निम्नानुसार UIVideoEditorController के साथ प्रयास किया।UIVideoEditorController प्रदर्शित किए बिना वीडियो ट्रिम करें?

UIVideoEditorController* videoEditor = [[[UIVideoEditorController alloc] init] autorelease]; 
    videoEditor.delegate = self; 
    NSString* videoPath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"MOV"]; 
    if ([UIVideoEditorController canEditVideoAtPath:videoPath]) 
    { 
     videoEditor.videoPath = videoPath; 
     [self presentModalViewController:videoEditor animated:YES]; 
    } 
    else 
    { 
     NSLog(@"can't edit video at %@", videoPath); 
    } 

लेकिन मुद्दा यह है कि उपरोक्त कोड सेब के वीडियो संपादक नियंत्रण प्रदर्शित करेगा और उपयोगकर्ता उस दृश्य पर कुछ संचालन कर सकता है। मैं इस दृश्य को प्रदर्शित नहीं करना चाहता, क्योंकि मैंने पहले ही MPMoviePlayer पर वीडियो प्रदर्शित किया है और कस्टम इनपुट पर वीडियो को ट्रिम करने के लिए उपयोगकर्ता इनपुट (प्रारंभ समय और समाप्ति समय) प्राप्त किया है। मैं UIVideoEditorController प्रदर्शित किए बिना एक वीडियो कैसे ट्रिम कर सकते हैं?

+1

यू मुझे वीडियो है, जहां उपयोगकर्ता प्रारंभ और समाप्ति समय चुन सकते हैं ट्रिमिंग के लिए उर कोड प्रदान कर सकते हैं? outputUrl छंटनी की वीडियो लिखने के लिए प्रदान की जाती है: –

उत्तर

17

अंत में मैं समाधान मिल गया।

हम UIVideoEditorController प्रदर्शित किए बिना वीडियो काट-छाँट के लिए AVAssetExportSession उपयोग कर सकते हैं।

मेरे कोड की तरह है:

- (void)checkExportSessionStatus:(AVAssetExportSession *)exportSession 
    { 

    [exportSession exportAsynchronouslyWithCompletionHandler:^(void) 
    { 

     switch ([exportSession status]) 
      { 

      case AVAssetExportSessionStatusCompleted: 

       NSLog(@"Export Completed"); 
       break; 

      case AVAssetExportSessionStatusFailed: 

       NSLog(@"Error in exporting"); 
       break; 

      default: 
       break; 

     } 
    }]; 
} 

मैं splitVideo विधि फोन कर रहा हूँ:

- (void)splitVideo:(NSString *)outputURL 
{ 

    @try 
    { 
     NSString *videoBundleURL = [[NSBundle mainBundle] pathForResource:@"Video_Album" ofType:@"mp4"]; 

     AVAsset *asset = [[AVURLAsset alloc] initWithURL:[NSURL fileURLWithPath:videoBundleURL] options:nil]; 

     NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:asset]; 

     if ([compatiblePresets containsObject:AVAssetExportPresetLowQuality]) 
     { 

      [self trimVideo:outputURL assetObject:asset]; 

     } 
     videoBundleURL = nil; 

     [asset release]; 
     asset = nil; 

     compatiblePresets = nil; 
    } 
    @catch (NSException * e) 
    { 
     NSLog(@"Exception Name:%@ Reason:%@",[e name],[e reason]); 
    } 
} 

इस विधि वीडियो

- (void)trimVideo:(NSString *)outputURL assetObject:(AVAsset *)asset 
    { 

    @try 
    { 

     AVAssetExportSession *exportSession = [[AVAssetExportSession alloc]initWithAsset:asset presetName:AVAssetExportPresetLowQuality]; 

     exportSession.outputURL = [NSURL fileURLWithPath:outputURL]; 

     exportSession.outputFileType = AVFileTypeQuickTimeMovie; 

     CMTime start = CMTimeMakeWithSeconds(splitedDetails.startTime, 1); 

     CMTime duration = CMTimeMakeWithSeconds((splitedDetails.stopTime - splitedDetails.startTime), 1); 

     CMTimeRange range = CMTimeRangeMake(start, duration); 

     exportSession.timeRange = range; 

     exportSession.outputFileType = AVFileTypeQuickTimeMovie; 

     [self checkExportSessionStatus:exportSession]; 

     [exportSession release]; 
     exportSession = nil; 

    } 
    @catch (NSException * e) 
    { 
     NSLog(@"Exception Name:%@ Reason:%@",[e name],[e reason]); 
    } 
} 

इस विधि ट्रिमिंग की स्थिति की जाँच करता है ट्रिम निर्यात बटन क्रिया विधि से और आउटपुट यूआर पास करता है तर्क के रूप में एल

+0

उत्पादन यूआरएल – Warewolf

+1

@Khoool क्या है। यह दस्तावेज़ निर्देशिका –

+0

में एक फ़ाइल पथ है, मैं एक ही कोड का उपयोग कर रहा हूं लेकिन त्रुटि प्राप्त कर रहा हूं: NSString * पथ = [NSSearchPathForDirectoriesInDomains (NSDocumentationDirectory, NSUserDomainMask, YES) ऑब्जेक्ट एट इंडेक्स: 0] निर्यात करने में त्रुटि; पथ = [पथ स्ट्रिंगबैपेंडिंगपैथकंपोनेंट: @ "new.mov"]; एनएसएलओजी (@ "वीडियो बचाने के लिए पथ% @", पथ); [स्वयं विभाजन वीडियो: पथ]; – Warewolf

2

हम आयात कर सकते हैं AVFoundation/AVFoundation.h

-(BOOL)trimVideofile 
{ 

    float videoStartTime;//define start time of video 
    float videoEndTime;//define end time of video 
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    [dateFormatter setDateFormat:@"yyyy-MM-dd_HH-mm-ss"]; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES); 
    NSString *libraryCachesDirectory = [paths objectAtIndex:0]; 
    libraryCachesDirectory = [libraryCachesDirectory stringByAppendingPathComponent:@"Caches"]; 
    NSString *OutputFilePath = [libraryCachesDirectory stringByAppendingFormat:@"/output_%@.mov", [dateFormatter stringFromDate:[NSDate date]]]; 
    NSURL *videoFileOutput = [NSURL fileURLWithPath:OutputFilePath]; 
    NSURL *videoFileInput;//<Path of orignal Video file> 

    if (!videoFileInput || !videoFileOutput) 
    { 
     return NO; 
    } 

    [[NSFileManager defaultManager] removeItemAtURL:videoFileOutput error:NULL]; 
    AVAsset *asset = [AVAsset assetWithURL:videoFileInput]; 

    AVAssetExportSession *exportSession = [AVAssetExportSession exportSessionWithAsset:asset 
                      presetName:AVAssetExportPresetLowQuality]; 
    if (exportSession == nil) 
    { 
     return NO; 
    } 
    CMTime startTime = CMTimeMake((int)(floor(videoStartTime * 100)), 100); 
    CMTime stopTime = CMTimeMake((int)(ceil(videoEndTime * 100)), 100); 
    CMTimeRange exportTimeRange = CMTimeRangeFromTimeToTime(startTime, stopTime); 

    exportSession.outputURL = videoFileOutput; 
    exportSession.timeRange = exportTimeRange; 
    exportSession.outputFileType = AVFileTypeQuickTimeMovie; 

    [exportSession exportAsynchronouslyWithCompletionHandler:^ 
    { 
     if (AVAssetExportSessionStatusCompleted == exportSession.status) 
     { 
      NSLog(@"Export OK"); 
     } 
     else if (AVAssetExportSessionStatusFailed == exportSession.status) 
     { 
      NSLog(@"Export failed: %@", [[exportSession error] localizedDescription]); 
     } 
    }]; 
    return YES; 
} 
+0

इस उत्तर के रूप में ऊपर ही है, इस सवाल का जवाब –

+0

यह नकल Answer.In यह स्थिति नहीं है डुप्लिकेट, हम एक और अलग अलग समाधान का उपयोग कर रहे का लाभ क्या है। –

+0

उपर्युक्त उत्तर में, यह 'AVAssetExportSession' का उपयोग कर रहा है। यहां भी वही बात है, तो क्या अंतर है? –

संबंधित मुद्दे