Reverse video playback in iOS

元气小坏坏 提交于 2019-12-18 04:03:19

问题


I want to play video backward in AVPlayer. I have tried with changing rates property to -1.0, and although it did work it was not smooth. Is there any way through which I can smoothly play videos backward?


回答1:


As stated in the comments, the problem is with keyframes and the fact that most codecs are not designed to play backwards. There are 2 options for re-encoding the video that doesn't require you to actually reverse the video in editing.

  1. Make every frame a keyframe. I've seen this work well for codecs like H.264 that rely on keyframes. Basically if every frame is a key frame, then each frame can be decoded without relying on any previous frames so it's effectively the same as playing forward.
  2. Use a codec that doesn't use keyframes and non-keyframes (basically all frames are always keyframes). PhotoJPEG is one such option, although I'm not completely sure if it plays back on iOS. I would think so. It works great on a Mac.

Note that either of this options will result in larger file sizes compared to typical "keyframe every x frames" encoded video.




回答2:


You have to reach at the end of the current item and then set the rate to the negative value. Something like this:

-(void)reversePlay { CMTime durTime = myPlayer.currentItem.asset.duration; if (CMTIME_IS_VALID(durTime)) { [myPlayer seekToTime:durTime toleranceBefore:kCMTimeZero toleranceAfter:kCMTimeZero]; [myPlayer setRate:-1.0]; } else NSLog(@"Invalid time"); }

source: https://stackoverflow.com/a/16104363/701043



来源:https://stackoverflow.com/questions/11902203/reverse-video-playback-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!