2013-11-27 13 views
5

मैं एक वीडियो फ़ाइल ट्रिम करना चाहता हूँ। मैं सिर्फ एक गैलरी से वीडियो चुनना चाहता हूं और इसे 15-सेकंड वीडियो में बदलना चाहता हूं। यदि मैं pickerviewcontroller के साथ सामान्य ट्रिमिंग का उपयोग करता हूं, तो यह समय निर्दिष्ट नहीं करता है और केवल फ्रेम दिखाता है, लेकिन मुझे 15 सेकंड तय करने की आवश्यकता है। इसे कैसे प्राप्त किया जा सकता है?वीडियो फ़ाइल को ट्रिम कैसे करें और आईओएस एसडीके के साथ 15 सेकंड वीडियो में कनवर्ट करें?

उत्तर

9
-(void)cropVideo:(NSURL*)videoToTrimURL{ 
    AVURLAsset *asset = [AVURLAsset URLAssetWithURL:videoToTrimURL options:nil]; 
    AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetHighestQuality]; 

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *outputURL = paths[0]; 
    NSFileManager *manager = [NSFileManager defaultManager]; 
    [manager createDirectoryAtPath:outputURL withIntermediateDirectories:YES attributes:nil error:nil]; 
    outputURL = [outputURL stringByAppendingPathComponent:@"output.mp4"]; 
    // Remove Existing File 
    [manager removeItemAtPath:outputURL error:nil]; 


    exportSession.outputURL = [NSURL fileURLWithPath:outputURL]; 
    exportSession.shouldOptimizeForNetworkUse = YES; 
    exportSession.outputFileType = AVFileTypeQuickTimeMovie; 
    CMTime start = CMTimeMakeWithSeconds(1.0, 600); // you will modify time range here 
    CMTime duration = CMTimeMakeWithSeconds(15.0, 600); 
    CMTimeRange range = CMTimeRangeMake(start, duration); 
    exportSession.timeRange = range; 
    [exportSession exportAsynchronouslyWithCompletionHandler:^(void) 
    { 
     switch (exportSession.status) { 
      case AVAssetExportSessionStatusCompleted: 
       [self writeVideoToPhotoLibrary:[NSURL fileURLWithPath:outputURL]]; 
       NSLog(@"Export Complete %d %@", exportSession.status, exportSession.error); 
       break; 
      case AVAssetExportSessionStatusFailed: 
       NSLog(@"Failed:%@",exportSession.error); 
       break; 
      case AVAssetExportSessionStatusCancelled: 
       NSLog(@"Canceled:%@",exportSession.error); 
       break; 
      default: 
       break; 
     } 

     //[exportSession release]; 
    }]; 
} 
+0

यदि हम समय और अवधि को बदलते हैं तो कभी-कभी यह वीडियो को सही नहीं कर देता है। क्या आप इसे जांच सकते हैं। – Imran

+0

@ram मेरी वीडियो अवधि 60 सेकंड है, मैं 15 से 30 सेकंड – sohil

+0

से हटा देना चाहता हूं सीएमटाइम प्रारंभ = सीएमटाइममेकविथसेकंड्स (15, 600); // आप सीएमटाइम अवधि = CMTimeMakeWithSeconds (30, 600) को संशोधित करेंगे; @sohil यह आपके लिए काम करेगा – ram

0

ऊपर दिए गए उत्तर में थोड़ा बदलाव के साथ मेरे लिए काम किया गया है, अगर हमें दोनों प्रारंभ समय और ट्रिम करने के लिए अंत समय निर्धारित करने की आवश्यकता है।

CMTime start = CMTimeMakeWithSeconds(1.0, 600); // you will modify time range here 
CMTime duration = CMTimeMakeWithSeconds(15.0, 600); 
CMTimeRange range = CMTimeRangeMake(start, duration); 

इस के लिए :

CMTime start = CMTimeMakeWithSeconds(self.StartTime, 600); // you will modify time range here 
    CMTime duration = CMTimeSubtract(CMTimeMakeWithSeconds(self.EndTime, 600), start); 
    CMTimeRange range = CMTimeRangeMake(start, duration); 

यह मेरे लिए काम किया

मैं इस बदल दिया है।

+0

मेरी वीडियो अवधि 60 सेकंड है, मैं 15 से 30 सेकंड – sohil

+0

से हटाना चाहता हूं सीएमटाइम प्रारंभ = सीएमटाइममेकविथसेकंड्स (15, 600); // आप सीएमटाइम अवधि = CMTimeMakeWithSeconds (30, 600) संशोधित करेंगे; @ सोहिल यह आपके लिए काम करेगा – ram

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