iPhone:jak uzyskać czas trwania wideo wybrany z biblioteki?
Używam UIImagePickerController
, aby wybrać plik wideo z biblioteki. A użytkownik może przesłać film.
Również używam właściwości videoMaximumDuration
podczas gdy użytkownik chce przechwycić wideo i przesłać je.
Chcę wiedzieć, jak Mogę uzyskać czas trwania wybranego pliku wideo ? tak, że mogę ograniczyć użytkownika do przesyłania wideo, które ma czas trwania więcej niż 20 sekund.
Jestem w stanie uzyskać podstawowe informacje o wybranym filmie za pomocą tego kodu:
- (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);
}
Ale nie ma nic o czasie trwania wybrany film.
Dzięki...6 answers
Mam rozwiązanie: używam klasy AVPlayerItem
oraz frameworka AVFoundation
i CoreMedia
.
#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);
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-01-02 10:40:54
Druga odpowiedź na to pytanie przy użyciu AVPlayerItem nie zadziałała dla mnie, ale to pomogło przy użyciu 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);
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-03-09 20:06:42
Swift 4
Niezależnie od użycia didFinishPickingMediaWithInfo
lub didFinishRecordingToOutputFileAtURL
możesz:
// needed only for didFinishPickingMediaWithInfo
let outputFileURL = info[UIImagePickerControllerMediaURL] as! URL
// get the asset
let asset = AVURLAsset(url: outputFileURL)
// get the time in seconds
let durationInSeconds = asset.duration.seconds
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-04-04 10:54:04
W przypadku, gdy używasz frameworków AVFoundation
i AssetLibrary, możesz wyliczyć wszystkie zasoby, zastosować filtr dla tylko filmów i uzyskać czas trwania każdego filmu za pomocą metody - (id)valueForProperty:(NSString *)property
. Pass in ALAssetPropertyDuration
for property. Poniższy kod wyświetla następujące informacje na konsoli.
Liczba klipu wideo 0 wynosi 66.8083333333334 sekund
Klip wideo numer 1 wynosi 190,06 sekundy
Wideoklip numer 2 trwa 13,74 sekundy
- (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];
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-10-30 04:56:58
Swift 3.0 i 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)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-12-07 18:17:52
Tak, możesz użyć właściwości "duration" zdefiniowanej przez MPMediaPlayerController. Plese wypróbuj i sprawdź wyjście. U można odwoływać się do Właściwości Czas trwania
Spróbuj użyć kontrolera MPMediaPlayerController do odtwarzania wideo, a następnie użyj właściwości "duration". U będzie w stanie wyraźnie uzyskać szczegóły dotyczące czasu trwania filmu.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-04-09 13:53:57