iOSでは、移動に使用するコーデック(H.264、MJPEGなど)、ムービーの幅と高さ、ファイルサイズ、およびムービーのフレーム数を取得する必要があります。映画の長さなどにAVAssetを使ってみましたが、長さは常にゼロでした。幅/高さも同じです。ムービープレーヤーコントローラーも使用してみましたが、それも機能しませんでした(以下のコードを参照)。ドキュメントは少し混乱していて、同じ場所に到達するための複数の方法では、同じ場所に到達することはできないようです。
上記の情報が正しく機能している人はいますか?私はいくつかのことを見逃していると確信していますが、サンプルコードやポインタを望んでいましたか?
編集:より良いコード例を追加しました。しかし、残っている質問があります。ムービーの作成日、それを圧縮するために使用されるコーデック、およびムービーのファイルサイズを取得するにはどうすればよいですか?誰かがこれらを理解しますか?
ありがとう
- (IBAction)getMovieInfo
{
int hours = 0, minutes = 0, seconds = 0;
NSURL* sourceMovieURL = [NSURL URLWithString:@"http://trailers.apple.com/movies/summit/stepuprevolution/stepuprevolution-tlr1_h480p.mov"];
AVURLAsset* movieAsset = [AVURLAsset URLAssetWithURL:sourceMovieURL options:nil];
NSArray *tracks = [movieAsset tracksWithMediaType:AVMediaTypeVideo];
if ([tracks count] != 0) {
AVAssetTrack *videoTrack = [tracks objectAtIndex:0];
NSTimeInterval durationSeconds = CMTimeGetSeconds([movieAsset duration]);
CGSize videoSize = videoTrack.naturalSize;
//
// Let's get the movie's meta data
//
// Start with the duration of the movie
hours = durationSeconds / 3600;
minutes = durationSeconds / 60;
seconds = (int)durationSeconds % 60;
durationLabel.text = [NSString stringWithFormat:@"%d:%d:%d", hours, minutes, seconds];
// Next is the creation (posting) date of the movie
//postedLabel.text = AVMetadataQuickTimeUserDataKeyCreationDate;
//The resolution of the movie
resolutionLabel.text = [NSString stringWithFormat:@"%g x %g", videoSize.width, videoSize.height];
// The frame rate of the movie
rateLabel.text = [NSString stringWithFormat:@"%g fps", [videoTrack nominalFrameRate]];
// The frame count of the movie
countLabel.text = [NSString stringWithFormat:@"%g", [videoTrack nominalFrameRate] * durationSeconds];
// Get the codec used to compress the movie
// And lastly, let's generate a thumbnail of the movie
AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:movieAsset];
if (imageGenerator != NULL) {
CMTime thumbPoint = CMTimeMakeWithSeconds(15.0, 600);
NSError *error = nil;
CGImageRef thumbnail = [imageGenerator copyCGImageAtTime:thumbPoint actualTime:nil error:&error];
if (thumbnail != NULL) {
// Convert CGImage thumbnail to UIImage and then scale it.
UIImage *tempImage = [[UIImage alloc] initWithCGImage:thumbnail];
if (tempImage != NULL) {
// Let's scale the image and put the it into the imageview.
self.thumbDisplay.image=[self scaleAndRotateImage:tempImage];
CGImageRelease(thumbnail);
}
}
}
}
}
- (UIImage *)scaleAndRotateImage:(UIImage *)image {
CGImageRef imgRef = image.CGImage;
CGFloat width = 135.0;
CGFloat height = 75.0;
CGAffineTransform transform = CGAffineTransformIdentity;
CGRect bounds = CGRectMake(0, 0, width, height);
CGFloat scaleRatio = bounds.size.width / width;
CGSize imageSize = CGSizeMake(CGImageGetWidth(imgRef), CGImageGetHeight(imgRef));
CGFloat boundHeight;
UIImageOrientation orient = image.imageOrientation;
switch (orient) {
case UIImageOrientationUp: //EXIF = 1
transform = CGAffineTransformIdentity;
break;
case UIImageOrientationUpMirrored: //EXIF = 2
transform = CGAffineTransformMakeTranslation(imageSize.width, 0.0);
transform = CGAffineTransformScale(transform, -1.0, 1.0);
break;
case UIImageOrientationDown: //EXIF = 3
transform = CGAffineTransformMakeTranslation(imageSize.width, imageSize.height);
transform = CGAffineTransformRotate(transform, M_PI);
break;
case UIImageOrientationDownMirrored: //EXIF = 4
transform = CGAffineTransformMakeTranslation(0.0, imageSize.height);
transform = CGAffineTransformScale(transform, 1.0, -1.0);
break;
case UIImageOrientationLeftMirrored: //EXIF = 5
boundHeight = bounds.size.height;
bounds.size.height = bounds.size.width;
bounds.size.width = boundHeight;
transform = CGAffineTransformMakeTranslation(imageSize.height, imageSize.width);
transform = CGAffineTransformScale(transform, -1.0, 1.0);
transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
break;
case UIImageOrientationLeft: //EXIF = 6
boundHeight = bounds.size.height;
bounds.size.height = bounds.size.width;
bounds.size.width = boundHeight;
transform = CGAffineTransformMakeTranslation(0.0, imageSize.width);
transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
break;
case UIImageOrientationRightMirrored: //EXIF = 7
boundHeight = bounds.size.height;
bounds.size.height = bounds.size.width;
bounds.size.width = boundHeight;
transform = CGAffineTransformMakeScale(-1.0, 1.0);
transform = CGAffineTransformRotate(transform, M_PI / 2.0);
break;
case UIImageOrientationRight: //EXIF = 8
boundHeight = bounds.size.height;
bounds.size.height = bounds.size.width;
bounds.size.width = boundHeight;
transform = CGAffineTransformMakeTranslation(imageSize.height, 0.0);
transform = CGAffineTransformRotate(transform, M_PI / 2.0);
break;
default:
[NSException raise:NSInternalInconsistencyException format:@"Invalid image orientation"];
}
UIGraphicsBeginImageContext(bounds.size);
CGContextRef context = UIGraphicsGetCurrentContext();
if (orient == UIImageOrientationRight || orient == UIImageOrientationLeft) {
CGContextScaleCTM(context, -scaleRatio, scaleRatio);
CGContextTranslateCTM(context, -height, 0);
} else {
CGContextScaleCTM(context, scaleRatio, -scaleRatio);
CGContextTranslateCTM(context, 0, -height);
}
CGContextConcatCTM(context, transform);
CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height), imgRef);
UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return imageCopy;
}