如何實現視頻的快進快退功能(轉)


原貼地址:https://blog.csdn.net/u012635648/article/details/76079415

======================================================

最近在研究視頻的播放的快進快退功能,先把相關的調研結果整理一下,做個記錄。

裸的H264碼流,如果實現快進快退必須基於 I 幀才能實現:在播放前對整個碼流進行統計,總共有多少幀,所有的 I 幀在什么位置。

在播放的時候,再根據用戶快進或快退的位置判斷相鄰最近的 I 幀在什么位置,然后從那一個 I 幀開始解碼播放。


H.264 要准確確定 I 幀比較麻煩。一個相對有效的方法是:對 slice 頭中的 first_mb_in_slice 和 slice_type 語法元素進行解析,以此來判斷是否 I 幀。

無論哪個播放器要想實現快進快退都只有按照上述方法來做。暴風影音也不例外。

如果一個視頻序列中只有第一幀是 I 幀,那么是根本無法實現快進快退的,暴風影音也不可能做到,不信你可以用暴風影音試試我附件里的這個裸流.

 

ffmpeg 提供了一個speed up/down的功能filter:

 

Speeding up/slowing down video

 

You can change the speed of a video stream using the ​setpts video filter. Note that in the following examples, the audio stream is not changed, so it should ideally be disabled with -an.

To double the speed of the video, you can use:

ffmpeg -i input.mkv -filter:v "setpts=0.5*PTS" output.mkv

The filter works by changing the presentation timestamp (PTS) of each video frame. For example, if there are two succesive frames shown at timestamps 1 and 2, and you want to speed up the video, those timestamps need to become 0.5 and 1, respectively. Thus, we have to multiply them by 0.5.

Note that this method will drop frames to achieve the desired speed. You can avoid dropped frames by specifying a higher output frame rate than the input. For example, to Go from an input of 4 FPS to one that is sped up to 4x that (16 FPS):

ffmpeg -i input.mkv -r 16 -filter:v "setpts=0.25*PTS" output.mkv

To slow down your video, you have to use a multiplier greater than 1:

ffmpeg -i input.mkv -filter:v "setpts=2.0*PTS" output.mkv

Speeding up/slowing down audio

You can speed up or slow down audio with the ​atempo audio filter. To double the speed of audio:

ffmpeg -i input.mkv -filter:a "atempo=2.0" -vn output.mkv

The atempo filter is limited to using values between 0.5 and 2.0 (so it can slow it down to no less than half the original speed, and speed up to no more than double the input). If you need to, you can get around this limitation by stringing multiple atempo filters together. The following with quadruple the audio speed:

ffmpeg -i input.mkv -filter:a "atempo=2.0,atempo=2.0" -vn output.mkv

Using a complex filtergraph, you can speed up video and audio at the same time:

ffmpeg -i input.mkv -filter_complex "[0:v]setpts=0.5*PTS[v];[0:a]atempo=2.0[a]" -map "[v]" -map "[a]" output.mkv


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM