void SetupReaderAndWriter() { NSError error = null; // Create asset reader and asset writer assetReader = AVAssetReader.FromAsset(asset, out error); if (assetReader == null) { throw new NSErrorException(error); } assetWriter = AVAssetWriter.FromUrl(outputURL, AVFileType.QuickTimeMovie, out error); if (assetWriter == null) { throw new NSErrorException(error); } // Create asset reader outputs and asset writer inputs for the first audio track and first video track of the asset // Grab first audio track and first video track, if the asset has them AVAssetTrack audioTrack = asset.TracksWithMediaType(AVMediaType.Audio).FirstOrDefault(); AVAssetTrack videoTrack = asset.TracksWithMediaType(AVMediaType.Video).FirstOrDefault(); SetupAssetReaderWriterForAudio(audioTrack); SetupAssetReaserWriterForVideo(videoTrack); }
public bool RestartReading() { var result = false; this.assetReader = AVAssetReader.FromAsset(this.videoAsset, out NSError error); if (error == null) { var settings = new AVVideoSettingsUncompressed { PixelFormatType = CVPixelFormatType.CV420YpCbCr8BiPlanarFullRange }; this.videoAssetReaderOutput = new AVAssetReaderTrackOutput(this.videoTrack, settings); if (this.videoAssetReaderOutput != null) { this.videoAssetReaderOutput.AlwaysCopiesSampleData = true; if (this.assetReader.CanAddOutput(this.videoAssetReaderOutput)) { this.assetReader.AddOutput(this.videoAssetReaderOutput); result = this.assetReader.StartReading(); } } } else { Console.WriteLine($"Failed to create AVAssetReader object: {error}"); } return(result); }
void ReadSampleBuffers(AVAsset asset) { NSError error; assetReader = AVAssetReader.FromAsset(asset, out error); if (error != null) { Console.WriteLine("Error creating Asset Reader: {0}", error.Description); } AVAssetTrack[] videoTracks = asset.TracksWithMediaType(AVMediaType.Video); AVAssetTrack videoTrack = videoTracks [0]; CreateDecompressionSession(videoTrack); var videoTrackOutput = AVAssetReaderTrackOutput.Create(videoTrack, (AVVideoSettingsUncompressed)null); if (assetReader.CanAddOutput(videoTrackOutput)) { assetReader.AddOutput(videoTrackOutput); } if (!assetReader.StartReading()) { return; } while (assetReader.Status == AVAssetReaderStatus.Reading) { CMSampleBuffer sampleBuffer = videoTrackOutput.CopyNextSampleBuffer(); if (sampleBuffer != null) { VTDecodeFrameFlags flags = VTDecodeFrameFlags.EnableAsynchronousDecompression; VTDecodeInfoFlags flagOut; decompressionSession.DecodeFrame(sampleBuffer, flags, IntPtr.Zero, out flagOut); sampleBuffer.Dispose(); if (presentationTimes.Count >= 5) { bufferSemaphore.Wait(); } } else if (assetReader.Status == AVAssetReaderStatus.Failed) { Console.WriteLine("Asset Reader failed with error: {0}", assetReader.Error.Description); } else if (assetReader.Status == AVAssetReaderStatus.Completed) { Console.WriteLine("Reached the end of the video."); ChangeStatus(); ReadSampleBuffers(asset); } } }