Get orientation from video or thumb taken with AVAssetImageGenerator

In my application, I create a video from separate images. Everything works fine, the videos are assembled correctly with the correct size and orientation. They are displayed correctly both in the Apple photo app and in MPMoviePlayer and in the isolated folder where I save them.
The problem arises when I try to get a thumb from a movie. The orientation is wrong, and I do not know how to fix it, I saw that there is a property - preferredTransform , but the result is the same for landscape and portrait video.
The url I'm using is the sandbox directory path. Here is a snippet:

 - (void) setVideoPath:(NSString *)videoPath { if (videoPath ==_videoPath) { return; } _videoPath = videoPath; AVAsset *asset = [AVAsset assetWithURL:[NSURL fileURLWithPath:_videoPath]]; AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc]initWithAsset:asset]; CMTime time = CMTimeMake(1, 1); CGImageRef imageRef = [imageGenerator copyCGImageAtTime:time actualTime:NULL error:NULL]; UIImage *thumbnail = [UIImage imageWithCGImage:imageRef]; CGImageRelease(imageRef); self.videoImageView.image = thumbnail; } 
+6
source share
4 answers

Just use this. No need to write any methods

 generator.appliesPreferredTrackTransform=true 
+38
source

SWIFT version

  // generate thumb from video and set it var err: NSError? = nil let asset = AVURLAsset(URL: NSURL(fileURLWithPath: videoPath! as String), options: nil) let imgGenerator = AVAssetImageGenerator(asset: asset) imgGenerator.appliesPreferredTrackTransform = true let cgImage: CGImage! do { cgImage = try imgGenerator.copyCGImageAtTime(CMTimeMake(0, 60), actualTime: nil) let thumbnail = UIImage(CGImage: cgImage) } catch let error as NSError { err = error cgImage = nil } 

The most important line for orientation:

 imgGenerator.appliesPreferredTrackTransform = true 
+1
source

At the moment, I found a trick, found a video rotation and applied it to a thumbnail. This seems to work, but I'm sure there is another way. The decision is taken from this gist , loans to Luca Bernardi.

 { //.. NSArray *videoTracks = [asset tracksWithMediaType:AVMediaTypeVideo]; AVAssetTrack* videoTrack = [videoTracks firstObject]; CGAffineTransform txf = [videoTrack preferredTransform]; if (txf.a == 0 && txf.b == 1.0 && txf.c == -1.0 && txf.d == 0) { thumbOrientation = UIImageOrientationRight; } if (txf.a == 0 && txf.b == -1.0 && txf.c == 1.0 && txf.d == 0) { thumbOrientation = UIImageOrientationLeft; } if (txf.a == 1.0 && txf.b == 0 && txf.c == 0 && txf.d == 1.0) { thumbOrientation = UIImageOrientationUp; } if (txf.a == -1.0 && txf.b == 0 && txf.c == 0 && txf.d == -1.0) { thumbOrientation = UIImageOrientationDown; } UIImage *thumbnail = [UIImage imageWithCGImage:imageRef scale:[[UIScreen mainScreen]scale] orientation:thumbOrientation]; //.. } 
0
source
 -(UIImage *)generateThumbImage : (NSURL *)url { AVAsset *asset = [AVAsset assetWithURL:url]; AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc]initWithAsset:asset]; imageGenerator.appliesPreferredTrackTransform = YES; CMTime time = [asset duration]; time.value = 0; CGImageRef imageRef = [imageGenerator copyCGImageAtTime:time actualTime:NULL error:NULL]; UIImage *thumbnail = [UIImage imageWithCGImage:imageRef]; CGImageRelease(imageRef); // CGImageRef won't be released by ARC return thumbnail; } 

Where url is the video.

0
source

Source: https://habr.com/ru/post/959228/


All Articles