2012-01-02 22 views
20

Tôi đang sử dụng UIImagePickerController để chọn tệp video từ thư viện. Và người dùng có thể tải video lên.iPhone: Cách nhận thời lượng video được chọn từ thư viện?

Ngoài ra tôi đang sử dụng videoMaximumDuration thuộc tính trong khi người dùng muốn quay video và tải video lên.

Tôi muốn biết rằng Làm thế nào tôi có thể nhận được thời lượng của tệp video được chọn? để tôi có thể hạn chế người dùng tải lên video có thời lượng hơn 20 giây.

tôi có thể nhận được một số thông tin cơ bản về video được chọn bởi mã này:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
    selectedVideoUrl = [info objectForKey:UIImagePickerControllerMediaURL]; 
    NSError *error; 
    NSDictionary * properties = [[NSFileManager defaultManager] attributesOfItemAtPath:selectedVideoUrl.path error:&error]; 
    NSNumber * size = [properties objectForKey: NSFileSize]; 
    NSLog(@"Vide info :- %@",properties); 
} 

Nhưng không có gì về thời gian của video được chọn là.

Thanks ...

Trả lời

31

Got giải pháp: Tôi sử dụng AVPlayerItem lớp và AVFoundationCoreMedia khuôn khổ.

#import <AVFoundation/AVFoundation.h> 
#import <AVFoundation/AVAsset.h> 

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
    selectedVideoUrl = [info objectForKey:UIImagePickerControllerMediaURL]; 

    AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:selectedVideoUrl]; 

    CMTime duration = playerItem.duration; 
    float seconds = CMTimeGetSeconds(duration); 
    NSLog(@"duration: %.2f", seconds); 
} 
+1

Bạn cũng cần phải liên kết khuôn khổ CoreMedia cho CMTimeGetSeconds hoạt động tốt. –

+0

chúng tôi có thể cắt một video cho thời lượng cụ thể không? – iEinstein

+0

@AshutoshMishra: http://stackoverflow.com/a/7239164/616094 – Maulik

1

Có, bạn có thể sử dụng thuộc tính "thời lượng" được xác định bởi MPMediaPlayerController. Plese thử nó ra và kiểm tra đầu ra. Bạn có thể tham khảo tại đây duration property

Cố gắng sử dụng MPMediaPlayerController để phát video và sau đó sử dụng thuộc tính "thời lượng". U sẽ có thể nhận được thông tin chi tiết rõ ràng về thời lượng của video.

2

Trong trường hợp bạn sử dụng khung làm việc AVFoundation và AssetLibrary, bạn có thể liệt kê tất cả nội dung, áp dụng bộ lọc chỉ cho video và nhận thời lượng của mỗi video với phương thức - (id)valueForProperty:(NSString *)property. Vượt qua số ALAssetPropertyDuration cho tài sản. Mã dưới đây in ra sau đây trên bàn điều khiển.

video clip số 0 là 66,80833333333334 giây

video clip số 1 là 190,06 giây

video clip số 2 là 13,74 giây

- (void)viewDidLoad { 
[super viewDidLoad]; 
// Do any additional setup after loading the view. 

if (!assetItems) { 
    assetItems = [[NSMutableArray alloc] init]; 
} else { 
    [assetItems removeAllObjects]; 
} 

if (!assetLibrary) { 
    assetLibrary = [[ALAssetsLibrary alloc] init]; 
} 

ALAssetsLibraryGroupsEnumerationResultsBlock listBlock = ^(ALAssetsGroup *group, BOOL *stop) { 
    if (group) { 
     [group setAssetsFilter:[ALAssetsFilter allVideos]]; 
     [group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) { 
      if (result) { 
       [assetItems addObject:result]; 
       NSString *duration = [result valueForProperty:ALAssetPropertyDuration]; 
       NSLog(@"video clip number %d is %@ seconds\n",index, duration); 
      } 
     }]; 
    } 
}; 

ALAssetsLibraryAccessFailureBlock failBlock = ^(NSError *error) { // error handler block 
    NSString *errorTitle = [error localizedDescription]; 
    NSString *errorMessage = [error localizedRecoverySuggestion]; 
    NSLog(@"%@...\n %@\n",errorTitle,errorMessage); 
}; 
[assetLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:listBlock failureBlock:failBlock]; 
} 
28

Câu trả lời khác cho câu hỏi này sử dụng AVPlayerItem didn' t làm việc cho tôi, nhưng điều này đã sử dụng AVURLAsset:

#import <AVFoundation/AVFoundation.h> 

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
    NSURL *videoURL=[info objectForKey:@"UIImagePickerControllerMediaURL"]; 
    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil]; 

    NSTimeInterval durationInSeconds = 0.0; 
    if (asset) 
     durationInSeconds = CMTimeGetSeconds(asset.duration); 
} 
+0

Tương tự ở đây! Điều này làm việc như một say mê. Thx – Bobjt

+0

Điều này như của iOS8 là câu trả lời đúng! – elliotrock

+0

Đây là câu trả lời đúng. – Ibdakine

5

Swift

bất kể sử dụng didFinishPickingMediaWithInfo hoặc didFinishRecordingToOutputFileAtURL bạn có thể:

// needed only for didFinishPickingMediaWithInfo 
let outputFileURL = info[UIImagePickerControllerMediaURL] as! NSURL 

// get the asset 
let asset = AVURLAsset(URL: outputFileURL) 

// get the time in seconds 
let durationInSeconds = asset.duration.seconds 
+1

Cảm ơn bạn đã trả lời. –

2

Swift 3.0 và Swift 4

let outputFileURL = info[UIImagePickerControllerMediaURL] as! URL 
// get the asset 
let asset = AVURLAsset.init(url: outputFileURL) // AVURLAsset.init(url: outputFileURL as URL) in swift 3 
// get the time in seconds 
let durationInSeconds = asset.duration.seconds 
print("==== Duration is ",durationInSeconds) 
+0

thanks..helpful @khalsa g –

Các vấn đề liên quan