简体   繁体   English

Python OpenCv:如何在视频上写文字? output.mp4 空

[英]Python OpenCv: How to Write text on video? output .mp4 empty

I would like to insert a text in a video dynamically.我想在视频中动态插入文本。 When I run the code below it displays a window with the text inside the video, but the.mp4 output generated file is empty (0kb)当我运行下面的代码时,它会显示一个 window 和视频内的文本,但是 .mp4 output 生成的文件是空的(0kb)

EDIT: problem persists after right indentation编辑:右缩进后问题仍然存在

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()

this is simply an indentation error.这只是一个缩进错误。 (and it's got nothing to do with writing text !) (这与写文字无关!)

you want out.write(frame) inside the loop, not after it (currently it only writes a single frame)你想out.write(frame)循环内,而不是在它之后(目前它只写一个帧)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM