2012-05-18 21 views
22

में सीएमटाइम को मानव पठनीय समय में कनवर्ट करना तो मेरे पास एक वीडियो से सीएमटाइम है। मैं फ़ोटो एप में वीडियो समय अवधि लेबल में इसे एक अच्छी स्ट्रिंग में कैसे परिवर्तित करूं? क्या कुछ सुविधा विधियां हैं जो इसे संभालती हैं? धन्यवाद।उद्देश्य-सी

AVURLAsset* videoAsset = [AVURLAsset URLAssetWithURL:url options:nil]; 
CMTime videoDuration = videoAsset.duration; 
float videoDurationSeconds = CMTimeGetSeconds(videoDuration); 

उत्तर

4

उदाहरण के लिए आप NSDate उपयोग कर सकते हैं और यह वर्णन विधि है। आप इच्छित आउटपुट प्रारूप निर्दिष्ट कर सकते हैं।

> ` 
// First, create NSDate object using 
NSDate* d = [[NSDate alloc] initWithTimeIntervalSinceNow:seconds]; 
// Then specify output format 
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"HH:mm:ss"]; 
// And get output with 
NSString* result = [dateFormatter stringWithDate:d];` 
+0

आप कुछ कोड का उपयोग कर निर्दिष्ट कर सकते हैं? मैं यहाँ एक नोब हूँ। धन्यवाद। का उपयोग कर – randomor

+2

पहले, बनाने NSDate वस्तु 'NSDate * घ = [[NSDate alloc] initWithTimeIntervalSinceNow: सेकंड];' फिर उत्पादन प्रारूप 'NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init] निर्दिष्ट; [dateFormatter setDateFormat: @ "HH: mm: ss"]; ' और साथ ' NSString * परिणाम = [dateFormatter stringWithDate: d] उत्पादन मिलता है, ' – Sangvin

+0

धन्यवाद। यह काम करता है, मैंने अंतिम विधि के लिए stringFromDate का उपयोग किया, लेकिन समस्या यह है कि समय अवधि 4.168334 थी, फिर भी आउटपुट 0:36:15 है ... क्या मुझे कुछ याद आया? – randomor

12

आप CMTimeCopyDescription का उपयोग कर सकते हैं, यह वास्तव में अच्छा काम करता है।

NSString *timeDesc = (NSString *)CMTimeCopyDescription(NULL, self.player.currentTime); 
NSLog(@"Description of currentTime: %@", timeDesc); 

संपादित करें: ठीक है, मैं सवाल बहुत तेजी से पढ़ते हैं, यह है कि क्या नहीं अपने चाहता था लेकिन debuging के लिए वैसे भी सहायक हो सकता है।

संपादित करें: @bcattle टिप्पणी के रूप में, मैंने सुझाए गए कार्यान्वयन में एआरसी के साथ एक स्मृति रिसाव शामिल है। यहाँ सही संस्करण:

NSString *timeDesc = (NSString *)CFBridgingRelease(CMTimeCopyDescription(NULL, self.player.currentTime)); 
NSLog(@"Description of currentTime: %@", timeDesc); 
+1

हाँ! सरल डीबबिंग के लिए बिल्कुल सही, और सीएमटाइम रेंजकॉपी डिस्क्रिप्शन भी, धन्यवाद! – Firula

+3

यह बहुत अच्छा है! माइनर अद्यतन, एक पाट डाली क्या करने की जरूरत: '(NSString *) CFBridgingRelease (CMTimeCopyDescription (शून्य, self.player.currentTime))' ' – bcattle

38

आप इस रूप में अच्छी तरह एक पाठ प्रारूप में एक वीडियो अवधि पाने के लिए उपयोग कर सकते हैं यदि आप न एक दिनांक स्वरूप

AVURLAsset *videoAVURLAsset = [AVURLAsset assetWithURL:url]; 
CMTime durationV = videoAVURLAsset.duration; 

NSUInteger dTotalSeconds = CMTimeGetSeconds(durationV); 

NSUInteger dHours = floor(dTotalSeconds/3600); 
NSUInteger dMinutes = floor(dTotalSeconds % 3600/60); 
NSUInteger dSeconds = floor(dTotalSeconds % 3600 % 60); 

NSString *videoDurationText = [NSString stringWithFormat:@"%i:%02i:%02i",dHours, dMinutes, dSeconds]; 
+1

dTotalSeconds% 3600% 60' भी है' dTotalSeconds% 60' :) –

11

आवश्यकता सवाल और ऊपर टिप्पणियों के संयोजन पर आधारित , इस संक्षिप्त है:

AVURLAsset* videoAsset = [AVURLAsset URLAssetWithURL:url options:nil]; 
CMTime videoDuration = videoAsset.duration; 
float videoDurationSeconds = CMTimeGetSeconds(videoDuration); 

NSDate* date = [NSDate dateWithTimeIntervalSince1970:videoDurationSeconds]; 
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]]; 
[dateFormatter setDateFormat:@"HH:mm:ss"]; //you can vary the date string. Ex: "mm:ss" 
NSString* result = [dateFormatter stringFromDate:date]; 
+2

मैंने कुछ प्रदर्शन परीक्षण किया और NSDateFormatter का उपयोग NSString stringWithFormat (ceekay का उत्तर) का उपयोग करने से 120 गुना धीमा है। –

+0

क्योंकि आपको बार-बार फॉर्मेटर्स नहीं बनाना चाहिए। वे बनाने के लिए महंगा हैं। इसे एक बार बनाएं और इसे चारों ओर रखें। – uchuugaka

1

यहाँ cmtime से सेकंड प्राप्त करने के लिए कोड है

NSLog(@"seconds = %f", CMTimeGetSeconds(cmTime)); 
5

वहाँ हमेशा है एक विस्तार;)

import CoreMedia 

extension CMTime { 
    var durationText:String { 
     let totalSeconds = CMTimeGetSeconds(self) 
     let hours:Int = Int(totalSeconds/3600) 
     let minutes:Int = Int(totalSeconds % 3600/60) 
     let seconds:Int = Int(totalSeconds % 60) 

     if hours > 0 { 
      return String(format: "%i:%02i:%02i", hours, minutes, seconds) 
     } else { 
      return String(format: "%02i:%02i", minutes, seconds) 
     } 
    } 
} 

videoPlayer?.addPeriodicTimeObserverForInterval(CMTime(seconds: 1, preferredTimescale: 1), queue: dispatch_get_main_queue()) { time in 
    print(time.durationText) 
} 
0

स्विफ्ट 3 उपयोग करने के लिए:

let time = kCMTimeZero 
let timeString = time.toString() 
+1

क्या आप थोड़ा सा विस्तार कर सकते हैं? – Adrian

+0

कैसे इस कोड काम करता है और सिर्फ कोड देना नहीं है, एक स्पष्टीकरण के रूप में अधिक भविष्य पाठकों मदद करने के लिए की संभावना है समझाने के लिए [संपादित करें] लिंक का उपयोग करें। यह भी देखें [उत्तर]। [स्रोत] (http://stackoverflow.com/users/5244995) –

1

स्विफ्ट 3.0 ios 10 जवाब आधारित codingrhythm जवाब ...

extension CMTime { 
var durationText:String { 
    let totalSeconds = CMTimeGetSeconds(self) 
    let hours:Int = Int(totalSeconds/3600) 
    let minutes:Int = Int(totalSeconds.truncatingRemainder(dividingBy: 3600)/60) 
    let seconds:Int = Int(totalSeconds.truncatingRemainder(dividingBy: 60)) 

    if hours > 0 { 
     return String(format: "%i:%02i:%02i", hours, minutes, seconds) 
    } else { 
     return String(format: "%02i:%02i", minutes, seconds) 
    } 
    } 
} 
+0

एक गलती है, मिनट के लिए नहीं है, होना चाहिए: इंट (totalSeconds.truncatingRemainder (dividingBy: 3600)/60) –

+0

धन्यवाद नाडी, यह अद्यतन किया गया। – user3069232

0

एक आसान तरीका है (NSDate और NSDateFormatter का उपयोग किए बिना) यह करने के लिए: -

स्विफ्ट का प्रयोग: -

 func updateRecordingTimeLabel() 
     { 

    // Result Output = MM:SS(01:23) 
    let cmTime = videoFileOutput.recordedDuration 
        var durationInSeconds = Int(CMTimeGetSeconds(cmTime)) 
        let durationInMinutes = Int(CMTimeGetSeconds(cmTime)/60) 
        var strDuMin = String(durationInMinutes) 

        durationInSeconds = durationInSeconds-(60*durationInMinutes) 
        var strDuSec = String(durationInSeconds) 

        if durationInSeconds < 10 
        { 
         strDuSec = "0"+strDuSec 
        } 
        if durationInMinutes < 10 
        { 
         strDuMin = "0"+strDuMin 
        } 
    // Output string 
    let str_output = strDuMin+":"+strDuSec 
print("Result Output : [\(str_output)]") 

}