我想在視頻中動態插入文本。當我運行下面的代碼時,它會顯示一個視窗,其中包含視頻中的文本,但 .mp4 輸出生成的檔案為空(0kb)
編輯:右縮進后問題仍然存在
path = r'C:\\Users\\semnome\\Desktop\\automatizacoes\\m\\'
video_file = 'a.mp4'
x = True
cap = cv.VideoCapture(video_file) #cap for "Video Capture Object"
fourcc = cv.VideoWriter_fourcc(*'mp4v')
out = cv.VideoWriter('outpu2t.mp4', fourcc, 20.0, (640,480))
try:
while(True):
# Capture frames in the video
ret, frame = cap.read()
font = cv.FONT_HERSHEY_SIMPLEX
cv.putText(frame,
'TEXT ON VIDEO',
(50, 50),
font, 1,
(0, 640, 480),
2,
cv.LINE_4)
out.write(frame)
cv.imshow('video', frame)
if cv.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv.destroyAllWindows()
except:
print("Video has ended.")
cap.release()
cv.destroyAllWindows()
uj5u.com熱心網友回復:
這只是一個縮進錯誤。(這與寫文字無關!)
你想out.write(frame)
在回圈內,而不是在它之后(目前它只寫一個幀)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/506600.html
下一篇:如何在影像中找到像素顏色?