解决Python OpenCV 读取视频并抽帧出现error while decoding的问题
生活随笔
收集整理的這篇文章主要介紹了
解决Python OpenCV 读取视频并抽帧出现error while decoding的问题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
解決Python OpenCV 讀取視頻抽幀出現error while decoding的問題
- 1. 問題
- 2. 解決
- 3. 源代碼
- 參考
1. 問題
讀取H264視頻,抽幀視頻并保存,報錯如下;
[aac @ 00000220b9a07fc0] Input buffer exhausted before END element found
[h264 @ 00000220b9cd0500] error while decoding MB 20 45, bytestream -14
2. 解決
溯本求源:https://stackoverflow.com/questions/49233433/opencv-read-errorh264-0x8f915e0-error-while-decoding-mb-53-20-bytestream
發現問題原因是:它與時間有關,當在連續的capture.read()之間執行比較耗時的操作時會出現該錯誤。
解決:增加一個線程處理捕獲到的視頻幀就好~~~
3. 源代碼
import os
import queue
import threadingimport cv2q = queue.Queue()
save_path = ''def get_frame_from_video(video_name, interval):"""Args:video_name:輸入視頻名字interval: 保存圖片的幀率間隔Returns:"""# 開始讀視頻video_capture = cv2.VideoCapture(video_name)i = 0while True:success, frame = video_capture.read()# 檢測是否到了視頻尾部if not success or frame is None:print('video is all read')breaki += 1if i % interval == 0:q.put(frame)def Display(arr):print("Start Displaying")j = int(arr)while True:if q.empty() != True:frame = q.get()# 保存圖片j += 1save_name = save_path + str(j) + '.jpg'cv2.imwrite(save_name, frame)print('image of %s is saved' % save_name)# cv2.imshow(save_name, frame)if cv2.waitKey(1) & 0xFF == ord('q'):breakif __name__ == '__main__':video_name = 'F:\\v01.H264'j = 0save_path = video_name.split('.H264')[0] + "\\"print(save_path)interval = 125# 保存圖片的路徑is_exists = os.path.exists(save_path)if not is_exists:os.makedirs(save_path)print('path of %s is build' % save_path)p2 = threading.Thread(target=Display,args=[j])p2.start()get_frame_from_video(video_name, interval)
參考
- https://stackoverflow.com/questions/49233433/opencv-read-errorh264-0x8f915e0-error-while-decoding-mb-53-20-bytestream
- https://blog.csdn.net/darkeyers/article/details/84865363
總結
以上是生活随笔為你收集整理的解决Python OpenCV 读取视频并抽帧出现error while decoding的问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何写好中考作文的结尾呢?
- 下一篇: 鼋头渚特惠票多少钱