exporting CALayer with AVMutableVideoComposition
yes I see the issue, thanks for the code BTW! I have been trying to do this, and I just copy paste (I'm a noob) but I found your issue while editing the code for my issue, this line
NSString *urlStr = [[NSBundle mainBundle] pathForResource:@"video-1.mp4" ofType:nil];
that is wrong, funny kind of, anyways change it to this
NSString *urlStr = [[NSBundle mainBundle] pathForResource:@"video-1" ofType:@"mp4"];
and that should be the only issue. the reason its black is because it doesn't have the right movie.
AVMutableVideoComposition doesn't play back
the answer to my question is quite simple: just update to iOS 9.3
Apple obviously has a broken framework at the moment.
There are still cases, where it doesn't work perfectly, but it's fine.
I guess it's going to be fixed in 9.4
Bye!
Andreas
Exporting a mirrored video with AVMutableComposition causes resizing issues
There is an active bug that prevents you from exporting mirrored videos correctly. You need a few workarounds:
- Turn off the mirroring on the movieOutputFile
Manually flip the video horizontally when needed:
if needsMirroring == true {
var transform:CGAffineTransform = CGAffineTransform(scaleX: -1.0, y: 1.0)
transform = transform.translatedBy(x: -naturalSize.width, y: 0.0)
transform = transform.rotated(by: CGFloat(Double.pi/2))
transform = transform.translatedBy(x: 0.0, y: -naturalSize.width)
videoTransform = transform
}
It took me days to figure this out, hope it helps.
Update AVMutableVideoComposition on AVPlayerItem faster than video framerate
This is a decent solution I managed to find.
I noticed that putting a sleep in the frame processing block actually seemed to improve the perceived performance.
The AVMutableVideoComposition
must build up a buffer of frames and the delay I'm seeing is that buffer running out before the frames with new filter values show up. Sleeping in the frame processing block prevented the buffer from filling up making the changes show up immediately.
I looked through the documentation of AVMutableVideoComposition
for the millionth time and found this little gem in the docs for sourceTrackIDForFrameTiming
.
If an empty edit is encountered in the source asset’s track, the compositor composes frames as needed up to the frequency specified in frameDuration property. Otherwise the frame timing for the video composition is derived from the source asset's track with the corresponding ID.
I had previously tried setting the frameDuration
on the composition but couldn't get it to go faster than the video's framerate. If I set the sourceTrackIDForFrameTiming to kCMPersistentTrackID_Invalid
it actually lets me speed up the framerate.
By setting the framerate to be extremely high (1000 fps), the phone can never fill up the buffer, making the changes appear immediate.
composition.sourceTrackIDForFrameTiming = kCMPersistentTrackID_Invalid
let frameRateTooHighForPhone = CMTime(value: 1, timescale: 1000)
composition.frameDuration = frameRateTooHighForPhone
It's a little bit hackier than is ideal, but it's not a bad solution.
Export a AVMutableVideoComposition using AVAssetWriter instead of AVAssetExportSession
Set up an AVAssetReader
to "read" the video frames from the AVMutableVideoComposition
, and then hand them off to the AVAssetWriterInput
. You would set up the asset reader with an AVAssetReaderVideoCompositionOutput
, setting the videoComposition
, and possibly the customVideoCompositor
if needed. The custom video compositor is where you can involve your own CIFilters and anything else you want.
Related Topics
How to Get the Nondecoded Attributes from a Decoder Container in Swift 4
Libmobilegestalt Mobilegestalt.C:890: Mgisdeviceoneoftype Is Not Supported on This Platform
How to Send Msmessage in Messages Extension
How to Run a Task in Swift on a Particular Date-Time in Background Either Application Is on or Off
Swift - Convert Values in Array to Doubles or Floats
Mpmovieplayercontroller Stops Playing After 5 Seconds - Swift
Swift: Programmatically Create Uilabel Fixed Width That Resizes Vertically According to Text Length
How to Change Button Text Size in iOS 8 Swift
Cocoa Singleton and Shared Instances
Gmsmarker Icon in the Top Left Corner of the View (Ios)
Embedding a Uitableview as a Container View Within a Uiviewcontroller
Apple Push Notifications in Tvos
How to Create a Rounded Rectangle Label in Xcode 7 and Swift 2