ios - How to get a specific video area in Swift AVPlayer? -


i working on swift base mac os-x application. user choose video file , nsview controller play video using avplayerview. user can draw rectangle on avplayerview , have achieved functionality. how show selected area video in rectangle camera preview layer @ right bottom?

here output: output pic

here code:

var videourl = nsurl() // video file url file chooser var videosize = nssize() // here video resolution size kept i.e. 720*480 var player:avplayerview! // avplayerview play video  var camerapreviewlayer:avplayerlayer! //the right bottom camera preview layer var rectanglelayer:avplayerlayer! // rectangle draw on avplayer while mouse dragging  var isclicked = false var startpoint = nspoint() // holds starting point location of mouse pointer while draging  var rect:nsrect!  override func viewdidload() {     super.viewdidload()      self.player = avplayerview()      self.player.frame.origin = cgpoint(x: 0, y: 0)     self.player.setframesize(self.videosize)      self.player.player = avplayer.playerwithurl(videourl) as! avplayer     self.view.addsubview(self.player)      setuprectangle()     setupcamerapreviewlayer() }  // intially setup white color rectangle draged on avplayer func setuprectangle() {     self.rectanglelayer = avplayerlayer()     self.rectanglelayer.backgroundcolor = nscolor.clearcolor().cgcolor     self.rectanglelayer.bordercolor = nscolor.whitecolor().cgcolor     self.rectanglelayer.borderwidth = 1.5     self.rectanglelayer.frame = self.player.bounds } // intially setup camerapreview layer show @ right bottom of avplayer func setupcamerapreviewlayer(){     camerapreviewlayer = avplayerlayer(player: self.player.player)      //place camerapreview layer @ right bottom     camerapreviewlayer.frame.origin.x = self.player.framesize.width-100     camerapreviewlayer.frame.origin.y = self.player.frame.origin.y-100     camerapreviewlayer.frame.size.width = 100     camerapreviewlayer.frame.size.height = 100 }  override func  mousedown(theevent: nsevent) {     startpoint = theevent.locationinwindow     isclicked = true     removecamerapreviewlayer() }  override func mousedragged(theevent: nsevent) {     var endpoint = theevent.locationinwindow      if( isclicked ){         rect = nsrect(x: startpoint.x, y: startpoint.y, width: -(startpoint.x-endpoint.x), height: -(startpoint.y-endpoint.y))         drawcustomrect(rect)     } }  override func  mouseup(theevent: nsevent) {     var endpoint = theevent.locationinwindow      if( isclicked ){  // think have magic code here         addcamerapreviewlayer()     }     isclicked = false; }  // redraw white color rectange on avplayer func drawcustomrect(rect: nsrect) {     self.rectanglelayer.frame = rect     self.player.layer?.addsublayer(self.rectanglelayer) }  // add camera previewlayer avplayer func addcamerapreviewlayer() {     self.player.layer?.addsublayer(self.layer) }  // remove camera previewlayer avplayer func removecamerapreviewlayer() {     self.camerapreviewlayer.removefromsuperlayer() } 

here desire output picture wants. desired output

suppose video has size 720*480, user has draw rectangle , points (x1, y1) (x2,y1) (x3,y3) (x4,y4). how crop video in camera preview layer(at right bottom) shows video of area same rectangle selected user?

any 1 how achieve functionality? have spend many days on , exhaust.

note: can in open-cv video processing using roi, requirement in native swift language.

what create container layer preview layer , create preview video layer sublayer of it. can adjust video preview layer size , position matches rectangle/big player ratio.

for try setup camera layer such:

func setupcamerapreviewlayer(){     // create camera preview layer container used mask preview layer     let cameracontainerlayer = avlayer()     cameracontainerlayer.masktobounds = true      //place cameracontainerlayer @ right bottom     cameracontainerlayer.frame.size.width = 100 // change whatever size you'd preview be. preview layer have same width/height ratio player layer     cameracontainerlayer.frame.size.height = cameracontainerlayer.frame.size.width * self.player.framesize.height / self.player.framesize.width     cameracontainerlayer.frame.origin.x = self.player.frame.size.width - cameracontainerlayer.frame.size.width     cameracontainerlayer.frame.origin.y = self.player.frame.size.height - cameracontainerlayer.frame.size.height      // create camera preview layer     camerapreviewlayer = avplayerlayer(player: self.player.player)      //place camerapreview layer container layer has same size ratio selected rectangle big preview layer     let widthratio = self.rectanglelayer.bounds.size.width / rect.size.width // rect cgrect of rectangle user draw     let heightratio = self.rectanglelayer.bounds.size.height / rect.size.height // rect cgrect of rectangle user draw      camerapreviewlayer.frame.size.width = cameracontainerlayer.bounds.size.width * widthratio     camerapreviewlayer.frame.size.height = cameracontainerlayer.bounds.size.height * heightratio     camerapreviewlayer.frame.origin.x = //calculate right x in here based on widthratio     camerapreviewlayer.frame.origin.y = //calculate right x in here based on widthratio       cameracontainerlayer.addsublayer(camerapreviewlayer) } 

something should trick

p.s: code above not tested and/or complete. it's should have started on right track


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - Bypass Geo Redirect for specific directories -

php - .htaccess mod_rewrite for dynamic url which has domain names -