Intro to AVFoundation | #inspect2014

Intro to AVFoundation | #inspect2014

Intro to Apple's AVFoundation

E49eab7677b4696db754b356953b9eac?s=128

Ivan Acosta-Rubio

June 25, 2014
Tweet

Transcript

  1. None
  2. @ivanacostarubio

  3. AVFoundation record, display, edit

  4. None
  5. github.com/ivanacostarubio/helu

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

    }! 5 @helu.winning = lambda { |transaction| }! 8 ! @helu.restore = lambda { ... }!
  7. ivanacostarubio/motion-social

  8. None
  9. AVFoundation audio + video

  10. None
  11. None
  12. @Dain Fagerholm

  13. AVFoundation audio + video FTFW!

  14. Capture Playback Edit

  15. OLD & HUGE

  16. IOS4 / OS10.7 Lion

  17. Almost Identical

  18. Screen Camera Microphone

  19. None
  20. 1 @tape = ScreenRecorder.new("my_file")! 2 @tape.start! 3 @tape.stop!

  21. 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
  22. Session AVCaptureSession Input AVCaptureScreenInput! AVCaptureDeviceInput

  23. ! 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! !
  24. 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
  25. 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
  26. Session AVCaptureSession Device AVCaptureScreenInput! AVCaptureDeviceInput Output AVCaptureMovieFileOutput

  27. 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! ! !
  28. None
  29. video.softwarecriollo.com

  30. UIKit CoreAudio AVFoundation CoreMedia CoreAnimation Media Player

  31. When not to use • Capture photo or video •

    Play a movie
  32. When to use • Inspect Media Properties • Custom UI

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

    AVPlayerItem, AVPlayerLayer) • Edit (AVMutableComposition) • Read / Write (AVExportSession)
  34. None
  35. UIView AVPlayerLayer AVPlayer CALayer insertSublayer

  36. 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
  37. UIView AVPlayerLayer AVPlayer CALayer insertSublayer

  38. None
  39. None
  40. 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!
  41. 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>
  42. AVURLAsset AVAssetExportSession AVMutableComposition

  43. 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!
  44. 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!
  45. RECAP

  46. None
  47. Session AVCaptureSession Device AVCaptureScreenInput! AVCaptureDeviceInput Output AVCaptureMovieFileOutput Recording

  48. UIView AVPlayerLayer AVPlayer CALayer insertSublayer Display

  49. AVURLAsset AVAssetExportSession AVMutableComposition Editing / Composing

  50. None