给定电影的URL,如何获取其信息? [英] Given a URL to a movie, how can retrieve it's info?

查看:140
本文介绍了给定电影的URL,如何获取其信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在iOS下,我需要获取用于进行移动的编解码器(H.264,MJPEG等),电影的宽度和高度,文件大小以及电影中的帧数。我尝试使用AVAsset作为电影的持续时间,但是持续时间始终为零。宽度/高度相同。我也尝试过使用电影播放器​​控制器,但是那也不起作用(请参见下面的代码)。这些文档有些令人困惑,并且到达目的地的方式多种多样,您似乎无法到达同一地点。

Under iOS, I need to get the codec used to make the move (H.264, MJPEG and so on), the movies' width and height, the file size, and the number of frames in the movie. I tried using AVAsset for the movie duration and such but the duration was always zero. Same for the width/height. I also tried using a movie player controller but that did not work either (see code below). The docs are a bit confusing and with multiple ways of getting to the same place you can't seem to get to the same spot.

有人能获得上述信息吗?正确吗?我确定我缺少一些东西,但是我希望获得示例代码和/或指针吗?

Has anyone got the above information working properly? I am sure I am missing a few things but I was hoping for sample code and/or pointers?

编辑:我添加了一个更好的代码示例。但是,仍然存在一些问题。如何获得电影的创建日期,用于压缩电影的编解码器以及电影的文件大小?有人知道了吗?

I added a better code example. But, there are questions that remain. How do I get the creation date of the movie, the codec used to compress it and the movie's file size? Anybody figure these out?

谢谢

- (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;
}


推荐答案

NSURL 的方法 getResourceValue:forKey:error 允许您获取字节总数:

NSURL's method getResourceValue:forKey:error allows you to get total number of bytes:

NSURL *fileUrl = [NSURL fileURLWithPath:filePath];
NSString *size = NULL;
[fileUrl getResourceValue:&size forKey:NSURLFileSizeKey error:nil];
NSLog(@"Bytes : %@",size);

检查是否有返回的错误是个好习惯。

It is good practice to check for any errors returned.

这篇关于给定电影的URL,如何获取其信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆