[英]First frame of a video using AVFoundation
I'm trying to get the first frame of a video using the classes in AVFoundation. 我正在尝试使用AVFoundation中的类来获取视频的第一帧。 But it appears to not be getting an image at all.
但它似乎根本没有得到一个图像。
My code currently looks like this 我的代码目前看起来像这样
AVURLAsset* asset = [AVURLAsset URLAssetWithURL:[NSURL URLWithString:videoPath] options:nil];
AVAssetImageGenerator* imageGenerator = [AVAssetImageGenerator assetImageGeneratorWithAsset:asset];
UIImage* image = [UIImage imageWithCGImage:[imageGenerator copyCGImageAtTime:CMTimeMake(0, 1) actualTime:nil error:nil]];
[videoFrame setImage:image];
The value of video path is /var/mobile/Applications/02F42CBF-D8BD-4155-85F2-8CF1E55B5023/Documents/videos/1334300431637030.mp4
which is definitely a video, since I can play it with MPMoviePlayerViewController
. 视频路径的值是
/var/mobile/Applications/02F42CBF-D8BD-4155-85F2-8CF1E55B5023/Documents/videos/1334300431637030.mp4
这绝对是一个视频,因为我可以用MPMoviePlayerViewController
播放它。 I'm not sure what I'm doing wrong but any suggestions would be helpful. 我不确定我做错了什么,但任何建议都会有所帮助。
Thanks. 谢谢。
I solved it. 我解决了 Apparently using
[NSURL fileURLWithPath:videoPath]
instead of [NSURL URLWithString:videoPath]
makes all the difference. 显然使用
[NSURL fileURLWithPath:videoPath]
而不是[NSURL URLWithString:videoPath]
会有所不同。
I used null0pointer solution but in some videos I recieved the first frame rotated. 我使用了null0pointer解决方案,但在一些视频中,我收到了第一帧旋转。 To resolve this issue I set to TRUE the appliesPreferredTrackTransform property of AVAssetImageGenerator.
要解决此问题,我将AVAssetImageGenerator的applyPreferredTrackTransform属性设置为TRUE。 And this code looks like this:
这段代码看起来像这样:
AVURLAsset* asset = [AVURLAsset URLAssetWithURL:fileURL options:nil];
AVAssetImageGenerator* imageGenerator = [AVAssetImageGenerator assetImageGeneratorWithAsset:asset];
[imageGenerator setAppliesPreferredTrackTransform:TRUE];
UIImage* image = [UIImage imageWithCGImage:[imageGenerator copyCGImageAtTime:CMTimeMake(0, 1) actualTime:nil error:nil]];
[self.imageView setImage:image];
Doing this in Swift 4.0 : 在Swift 4.0中这样做:
// Assumes you have a local `fileURL`
var avAsset = AVURLAsset(url: fileURL, options: nil)
var imageGenerator = AVAssetImageGenerator(asset: avAsset)
imageGenerator.appliesPreferredTrackTransform = true
var thumbnail: UIImage?
do {
thumbnail = try UIImage(cgImage: imageGenerator.copyCGImage(at: CMTime(seconds: 0, preferredTimescale: 1), actualTime: nil))
} catch let e as NSError {
print("Error: \(e.localizedDescription)")
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.