Swift4.2 grab screenshot from Video
I think it may be an issue with the filetype of the movie, as this works:
import UIKit
import AVFoundation
func thumbnailImageFor(fileUrl:URL) -> UIImage? {
let video = AVURLAsset(url: fileUrl, options: [:])
let assetImgGenerate = AVAssetImageGenerator(asset: video)
assetImgGenerate.appliesPreferredTrackTransform = true
let videoDuration:CMTime = video.duration
let durationInSeconds:Float64 = CMTimeGetSeconds(videoDuration)
let numerator = Int64(1)
let denominator = videoDuration.timescale
let time = CMTimeMake(value: numerator, timescale: denominator)
do {
let img = try assetImgGenerate.copyCGImage(at: time, actualTime: nil)
let thumbnail = UIImage(cgImage: img)
return thumbnail
} catch {
print(error)
return nil
}
}
let url: URL = URL(string: "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ElephantsDream.mp4")!
let image: UIImage? = thumbnailImageFor(fileUrl: url)
print("Image: \(image)")
iOS: AVPlayer - getting a snapshot of the current frame of a video
AVPlayerItemVideoOutput
works fine for me from an m3u8. Maybe it's because I don't consult hasNewPixelBufferForItemTime
and simply call copyPixelBufferForItemTime
? This code produces a CVPixelBuffer
instead of a UIImage
, but there are answers that describe how to do that.
This answer mostly cribbed from here
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()
@property (nonatomic) AVPlayer *player;
@property (nonatomic) AVPlayerItem *playerItem;
@property (nonatomic) AVPlayerItemVideoOutput *playerOutput;
@end
@implementation ViewController
- (void)setupPlayerWithLoadedAsset:(AVAsset *)asset {
NSDictionary* settings = @{ (id)kCVPixelBufferPixelFormatTypeKey : @(kCVPixelFormatType_32BGRA) };
self.playerOutput = [[AVPlayerItemVideoOutput alloc] initWithPixelBufferAttributes:settings];
self.playerItem = [AVPlayerItem playerItemWithAsset:asset];
[self.playerItem addOutput:self.playerOutput];
self.player = [AVPlayer playerWithPlayerItem:self.playerItem];
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
playerLayer.frame = self.view.frame;
[self.view.layer addSublayer:playerLayer];
[self.player play];
}
- (IBAction)grabFrame {
CVPixelBufferRef buffer = [self.playerOutput copyPixelBufferForItemTime:[self.playerItem currentTime] itemTimeForDisplay:nil];
NSLog(@"The image: %@", buffer);
}
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *someUrl = [NSURL URLWithString:@"http://qthttp.apple.com.edgesuite.net/1010qwoeiuryfg/sl.m3u8"];
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:someUrl options:nil];
[asset loadValuesAsynchronouslyForKeys:[NSArray arrayWithObject:@"tracks"] completionHandler:^{
NSError* error = nil;
AVKeyValueStatus status = [asset statusOfValueForKey:@"tracks" error:&error];
if (status == AVKeyValueStatusLoaded)
{
dispatch_async(dispatch_get_main_queue(), ^{
[self setupPlayerWithLoadedAsset:asset];
});
}
else
{
NSLog(@"%@ Failed to load the tracks.", self);
}
}];
}
@end
Related Topics
What Is a "Delegate" in Objective C's iPhone Development
Uibezierpath Triangle with Rounded Edges
Use Didselectrowatindexpath or Prepareforsegue Method for Uitableview
Native Facebook App Does Not Open with Facebook Login in iOS 9
Change Color of Back Button in Navigation Bar
Analysing Assets.Car File in iOS
Why Is Uicollectionviewcell's Outlet Nil
iPhone Ad Hoc Build Using Xcode 4
Ios: Uibutton Resize According to Text Length
How to Change Text on a Back Button
iOS Autolayout with Uiscrollview: Why Does Content View of Scroll View Not Fill the Scroll View
How to Display a Uipopoverview as a Annotation to the Map View? (iPad)
Difference Between _ and Self. in Objective-C
Showing Pushviewcontroller Animation Look Like Presentmodalviewcontroller
iPhone Read Uiimage (Frames) from Video with Avfoundation
What Is Prefix.Pch File in Xcode
Possible to Handle Your Own Http Url Schemes in iOS
Recording to Aac from Remoteio: Data Is Getting Written But File Unplayable