2012-06-25 41 views
5

Tôi đang cố tìm codec được sử dụng để nén phim. Tôi chắc chắn nếu tôi cần bằng cách nào đó sử dụng CMFormatDescription và nhận được một khóa CMVideoCodecType. Tôi đang mắc kẹt như thế nào để vượt qua các mảng siêu dữ liệu. Bất kỳ ý tưởng về làm thế nào để lấy codec?Truy xuất codec phim trong iOS?

AVURLAsset* movieAsset = [AVURLAsset URLAssetWithURL:sourceMovieURL options:nil]; 
NSArray *tracks = [movieAsset tracksWithMediaType:AVMediaTypeVideo]; 

if ([tracks count] != 0) { 
    AVAssetTrack *videoTrack = [tracks objectAtIndex:0]; 

    // 
    // Let's get the movie's meta data 
    // 

    // Find the codec 
    NSArray *metadata = [movieAsset commonMetadata]; 
} 
+0

bạn đã tìm thấy một giải pháp cho việc này không? – vale4674

Trả lời

3

Tôi nghĩ rằng nó chắc chắn là khó khăn hơn so với nó nên

#define FourCC2Str(code) (char[5]){(code >> 24) & 0xFF, (code >> 16) & 0xFF, (code >> 8) & 0xFF, code & 0xFF, 0} 

    if ([assetTrack.mediaType isEqualToString:AVMediaTypeVideo]) 
    { 
     for (id formatDescription in assetTrack.formatDescriptions) 
     { 
      NSLog(@"formatDescription: %@", formatDescription); 

      CMFormatDescriptionRef desc = (__bridge CMFormatDescriptionRef)formatDescription; 
      //CMMediaType mediaType = CMFormatDescriptionGetMediaType(desc); 

      // CMVideoCodecType is typedefed to CMVideoCodecType 

      CMVideoCodecType codec = CMFormatDescriptionGetMediaSubType(desc); 

      NSString* codecString = [NSString stringWithCString:(const char *)FourCC2Str(codec) encoding:NSUTF8StringEncoding]; 
      NSLog(@"%@", codecString); 

     } 
    } 
3

Một phiên bản nhẹ dễ đọc hơn các câu trả lời @ jbat100 (đối với những người như nhầm lẫn như ta đã ở cùng #define FourCC2Str rằng, hah)

// Get your format description from whichever track you want 
CMFormatDescriptionRef formatHint; 

// Get the codec and correct endianness 
CMVideoCodecType formatCodec = CFSwapInt32BigToHost(CMFormatDescriptionGetMediaSubType(formatHint)); 

// add 1 for null terminator 
char formatCodecBuf[sizeof(CMVideoCodecType) + 1] = {0}; 
memcpy(formatCodecBuf, &formatCodec, sizeof(CMVideoCodecType)); 

NSString *formatCodecString = @(formatCodecBuf); 
1

Cách tiếp cận Swift để truy xuất codec âm thanh và video được liên kết với phim:

func codecForVideoAsset(asset: AVURLAsset, mediaType: CMMediaType) -> String? { 
    let formatDescriptions = asset.tracks.flatMap { $0.formatDescriptions } 
    let mediaSubtypes = formatDescriptions 
     .filter { CMFormatDescriptionGetMediaType($0 as! CMFormatDescription) == mediaType } 
     .map { CMFormatDescriptionGetMediaSubType($0 as! CMFormatDescription).toString() } 
    return mediaSubtypes.first 
} 

Sau đó, bạn có thể chuyển vào số AVURLAsset của phim và kCMMediaType_Video hoặc kCMMediaType_Audio để truy xuất codec video và âm thanh tương ứng.

Chức năng toString() chuyển đổi FourCharCode đại diện của định dạng codec vào một chuỗi con người có thể đọc được, và có thể được cung cấp như một phương pháp khuyến nông trên FourCharCode:

extension FourCharCode { 
    func toString() -> String { 
     let n = Int(self) 
     var s: String = String (UnicodeScalar((n >> 24) & 255)) 
     s.append(UnicodeScalar((n >> 16) & 255)) 
     s.append(UnicodeScalar((n >> 8) & 255)) 
     s.append(UnicodeScalar(n & 255)) 
     return s.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet()) 
    } 
}