Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Intro to AVFoundation | #inspect2014

Intro to AVFoundation | #inspect2014

Intro to Apple's AVFoundation

Ivan Acosta-Rubio

June 25, 2014
Tweet

More Decks by Ivan Acosta-Rubio

Other Decks in Programming

Transcript

  1. 1 @helu = Helu.new("loosing_weight_10")! 2 @helu.fail = lambda { |transaction|

    }! 5 @helu.winning = lambda { |transaction| }! 8 ! @helu.restore = lambda { ... }!
  2. 1 class ScreenRecorder! 2 def initialize(file_name)! 3 end! 4 !

    5 def start! 6 end! 7 ! 8 def stop! 9 end! 10 ! 11 def setup_video! 12 end! 13 ! 14 def setup_audio! 15 end! 16 ! 17 def setup_recording! 18 end! 19 end
  3. ! 1 def setup_video! 2 @session = AVCaptureSession.alloc.init! 5 !

    6 @device = AVCaptureScreenInput.alloc.! 7 initWithDisplayID(CGMainDisplayID())! ! 10 if @session.canAddInput(@device)! 12 @session.addInput(@device)! 15 end! 16 ! 17 setup_audio! 18 @session.startRunning()! 19 end! ! ! ! ! 8 @device.capturesMouseClicks = true! 3 @session.sessionPreset = AVCaptureSessionPresetHigh! !
  4. 22 def setup_audio! 23 audioDevice = AVCaptureDevice.defaultDeviceWithMediaType(! 25 AVMediaTypeAudio)! 26

    audioInput = AVCaptureDeviceInput.deviceInputWithDevice(! 28 audioDevice, error:nil)! ! 29 if @session.canAddInput(audioInput)! 30 @session.addInput(audioInput)! 31 end! ! 32 end
  5. 1 class ScreenRecorder! 2 def initialize(file_name)! 3 end! 4 !

    5 def start! 6 end! 7 ! 8 def stop! 9 end! 10 ! 11 def setup_video! 12 end! 13 ! 14 def setup_audio! 15 end! 16 ! 17 def setup_recording! 18 end! 19 end
  6. 36 def setup_recording! 37 @captureMovieFileOutput = AVCaptureMovieFileOutput.new! 39 @captureMovieFileOutput.setDelegate(self)! !

    40 if @session.canAddOutput(@captureMovieFileOutput)! 42 @session.addOutput(@captureMovieFileOutput)! 43 end! ! 44 end! 45 ! 46 def start! 47 @captureMovieFileOutput. ! 48 startRecordingToOutputFileURL(video_path, ! 49 recordingDelegate:self)! 50 end! ! Stops Recording: @captureMovieFileOutput.stopRecording! ! !
  7. When to use • Inspect Media Properties • Custom UI

    • Compose / Combine media. • Detailed control over capture. • Adjust exposure, etc
  8. Class Groups • Capture (AVCaptureSession, AVCaptureMovieFileOutput) • Playback (AVAsset, AVPlayer,

    AVPlayerItem, AVPlayerLayer) • Edit (AVMutableComposition) • Read / Write (AVExportSession)
  9. 1 view = UIView.alloc.init! 2 player = AVPlayer.alloc.initWithURL(file_url)! 3 layer

    = AVPlayerLayer.playerLayerWithPlayer(player)! 5! 7 view.layer.insertSublayer(layer, atIndex:1)! 8! 10 player.play
  10. 1 @video_cut = VideoCut.new! 2 @video_cut.start_time = @start! 3 @video_cut.end_time

    = @end! 4 @video_cut.movie_file = "full_video"! 5 @video_cut.compose_movies!
  11. CMTime • It is not an object. It is a

    C Struck • value / timescale • CMTimeRangeMake(start_time, duration) ! (main)> CMTimeMakeWithSeconds(10,1)! => #<CMTime value=10 timescale=1 flags=1 epoch=0>
  12. 1 def asset! 2 @asset ||= AVURLAsset.URLAssetWithURL(file_url,options:nil) 4 end! !

    5 ! 6 def compose_movies! 7 @composition = AVMutableComposition.composition! 8! error = Pointer.new(:object)! 10 duration = CMTimeSubtract(end_time, start_time) ! 11 ! 12 ! 13 result = @composition.insertTimeRange(! 14 CMTimeRangeMake(start_time, duration)! 15 , ofAsset:asset, atTime:KCMTimeZero, ! 16 error: error)! 25 end!
  13. 1 def write_to_file! 2 @exportSession = AVAssetExportSession.alloc.! 3 initWithAsset(@composition, !

    4 presetName: ! 5 “AVAssetExportPreset640x480")! ! 6 @exportSession.outputURL = NSURL.fileURLWithPath(“name”)! 7 ! ! 9 @exportSession.outputFileType = "com.apple.quicktime-movie"! 10 ! 11 ! 12 c = Proc.new { ... }! 13 @exportSession.exportAsynchronouslyWithCompletionHandler(c)! 14 end!