ffmpeg是強大的多媒體處理工具,堪稱多媒體處理的瑞士軍刀,涵蓋了大量的多媒體處理工具。但是ffmpeg是由純C語言寫成,對于python用戶來說使用難度較高,為此今天向大家推薦一款在python中使用ffmpeg的開發包:PyAV
PyAV提供了ffmpeg的python接口,但實際是它只是使用ffmpeg做后端,使用Cython封裝了ffmpeg的接口,所以實際調用的還是ffmpeg。
PyAV安裝
PyAV是跨平臺的,可以根據自己的環境和平臺選擇安裝。
Windows安裝PyAV:
在Windows下安裝PyAV可以參照博客https://blog.csdn.net/Dillon2015/article/details/91358179
Mac OS X和Ubuntu上安裝PyAV:
在Mac OS X和Ubuntu上安裝PyAV可以參考官網安裝方法
PyAV使用
PyAV提供了非常方便的接口使開發者不需要太關注底層細節。
視頻分割為獨立的幀
有的時候做處理時需要將一段視頻按幀分成一張張圖像,在ffmpeg命令行中只需要一條命令:
ffmpeg –i test.avi –r 1 –f image2 image-%3d.jpeg
-r表示每秒提取圖像的數量,如果等于幀率則會將所有幀都提取出來。
在PyAV中實現同樣的功能也很簡單,
import av
?
container = av.open(path_to_video)
#path_to_video是你視頻的路徑
for frame in container.decode(video=0):
? ?frame.to_image().save('frame-%04d.jpg' % frame.index)
保存關鍵幀
對于一個視頻序列來說并不是所有幀都一樣,因為視頻編碼在進行幀間預測時會出現相互參考的情況,如果一幀的參考幀丟失或損壞了那么這一幀就無法正確解碼,所以對于那些用于被參考的幀就相對更重要了。
在
av.video.frame.VideoFrame
類中有一個屬性
key_frame
用以表示該幀是否是關鍵幀。
import av
import av.datasets
?
container = av.open(path_to_video)
# Signal that we only want to look at keyframes.
stream = container.streams.video[0]
stream.codec_context.skip_frame = 'NONKEY'
?
for frame in container.decode(stream):
? ?# We use `frame.pts` as `frame.index` won't make must sense with the `skip_frame`.
? ?frame.to_image().save(
? ? ? ?'night-sky.{:04d}.jpg'.format(frame.pts),
? ? ? ?quality=80,
? )
在以上代碼中跳過了非關鍵幀,將所有關鍵幀保存下來。
視頻轉封裝
視頻轉封裝就是改變視頻的封裝格式而不改變其中視頻流、音頻流等的編碼方式,例如從mp4->mkv
過程如下:
?
import av
import av.datasets
?
input_ = av.open(path_to_video)
output = av.open('remuxed.mkv', 'w')
?
# Make an output stream using the input as a template. This copies the stream
# setup from one to the other.
in_stream = input_.streams.video[0]
out_stream = output.add_stream(template=in_stream)
?
for packet in input_.demux(in_stream):
? ?# We need to skip the "flushing" packets that `demux` generates.
? ?if packet.dts is None:
? ? ? ?continue
?
? ?# We need to assign the packet to the new stream.
? ?packet.stream = out_stream
? ?output.mux(packet)
?
output.close()
生成視頻
PyAV還可以和numpy配合使用,直接將ndarray轉換成視頻幀,使得對幀的操作更加靈活和方便。
from __future__ import division
?
import numpy as np
?
import av
?
duration = 4
fps = 24
total_frames = duration * fps
container = av.open('test.mp4', mode='w')
stream = container.add_stream('mpeg4', rate=fps)
stream.width = 480
stream.height = 320
stream.pix_fmt = 'yuv420p'
?
for frame_i in range(total_frames):
? ?img = np.empty((480, 320, 3))
? ?img[:, :, 0] = 0.5 + 0.5 * np.sin(2 * np.pi * (0 / 3 + frame_i / total_frames))
? ?img[:, :, 1] = 0.5 + 0.5 * np.sin(2 * np.pi * (1 / 3 + frame_i / total_frames))
? ?img[:, :, 2] = 0.5 + 0.5 * np.sin(2 * np.pi * (2 / 3 + frame_i / total_frames))
?
? ?img = np.round(255 * img).astype(np.uint8)
? ?img = np.clip(img, 0, 255)
?
? ?frame = av.VideoFrame.from_ndarray(img, format='rgb24')
? ?for packet in stream.encode(frame):
? ? ? ?container.mux(packet)
?
#Flush stream
for packet in stream.encode():
? ?container.mux(packet)
?
#Close the file
container.close()
以上代碼生成了一段480x320幀率24fps的視頻。
小結
PyAV還要更多更強大的功能,感興趣的小伙伴可以自己安裝試試哦。
感興趣的請關注微信公眾號Video Coding
?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
