python - 在 OpenCV 中保存带有文本叠加的视频

标签 python opencv video vlc codec

我编写了一个脚本,用于从我的网络摄像头录制视频,在上面写入文本,然后保存到磁盘。但是,我似乎无法让它工作或无法在 VLC 中播放视频。

这是用于尝试所有编解码器/扩展类型组合的脚本,我运行它,反复按 ESC,查看是否适用于 VLC,然后对每个组合重复,直到脚本退出:

import cv2
import os

extensions = ['avi', 'mp4']
codecs = ['MJPG', 'H264', 'DIVX', 'XVID', 'X264']

for ext in extensions:
    for codec in codecs:
        savepath = "output__%s.%s" % (codec, ext)

        try:
            # setup up our webcam to read frames
            cap = cv2.VideoCapture(0)
            cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1920)
            cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080)

            # and our buffer to write frames
            fourcc = cv2.VideoWriter_fourcc(*codec)
            out = cv2.VideoWriter(savepath, fourcc, 20.0, (1080, 1920), 1)

            while True:
                ret, frame = cap.read()
                text = 'testing 123'
                font = cv2.FONT_HERSHEY_SIMPLEX
                cv2.putText(frame, text, (50, 50), font, 2, (255, 255, 0), 2)

                # write our frame
                out.write(frame)

                cv2.imshow('My Frame', frame)
                ch = cv2.waitKey(1)
                if ch == 27:  # ESC
                    break


            cap.release()
            out.release()
            cv2.destroyAllWindows()

            os.system("vlc %s" % savepath)

        except Exception as e:
            print "Combo codec=%s, exte=%s failed with: %s" % (codec, ext, str(e))

运行这个脚本的结果:

$ python test.py 
OpenCV Error: Assertion failed (img.cols == width && img.rows == height && channels == 3) in write, file /home/me/code/myproject/OpenCV-3.2.0/modules/videoio/src/cap_mjpeg_encoder.cpp, line 829
Combo codec=MJPG, exte=avi failed with: /home/me/code/myproject/OpenCV-3.2.0/modules/videoio/src/cap_mjpeg_encoder.cpp:829: error: (-215) img.cols == width && img.rows == height && channels == 3 in function write

VIDEOIO ERROR: V4L2: Pixel format of incoming image is unsupported by OpenCV
Unable to stop the stream: Device or resource busy
OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow, file /home/me/code/myproject/OpenCV-3.2.0/modules/highgui/src/window.cpp, line 312
Combo codec=H264, exte=avi failed with: /home/me/code/myproject/OpenCV-3.2.0/modules/highgui/src/window.cpp:312: error: (-215) size.width>0 && size.height>0 in function imshow

init done
opengl support available

For codec=DIVX, extension=avi
VLC media player 2.2.2 Weatherwax (revision 2.2.2-0-g6259d80)
[0000000001f0e148] core libvlc: Running vlc with the default interface. Use 'cvlc' to use vlc without interface.
[00007eff1cc017d8] avi demux error: no key frame set for track 0
QObject::~QObject: Timers cannot be stopped from another thread

For codec=XVID, extension=avi
VLC media player 2.2.2 Weatherwax (revision 2.2.2-0-g6259d80)
[00000000022b8148] core libvlc: Running vlc with the default interface. Use 'cvlc' to use vlc without interface.
[00007f9c14c017d8] avi demux error: no key frame set for track 0
QObject::~QObject: Timers cannot be stopped from another thread

For codec=X264, extension=avi
VLC media player 2.2.2 Weatherwax (revision 2.2.2-0-g6259d80)
[0000000001a55148] core libvlc: Running vlc with the default interface. Use 'cvlc' to use vlc without interface.
[00007f485cc017d8] avi demux error: no key frame set for track 0
QObject::~QObject: Timers cannot be stopped from another thread
OpenCV: FFMPEG: tag 0x47504a4d/'MJPG' is not supported with codec id 8 and format 'mp4 / MP4 (MPEG-4 Part 14)'
OpenCV: FFMPEG: fallback to use tag 0x0000006c/'l???'

For codec=MJPG, extension=mp4
VLC media player 2.2.2 Weatherwax (revision 2.2.2-0-g6259d80)
[0000000000e53148] core libvlc: Running vlc with the default interface. Use 'cvlc' to use vlc without interface.
[00007f02a8c017d8] mp4 demux error: cannot find any /moov/trak
QObject::~QObject: Timers cannot be stopped from another thread
OpenCV: FFMPEG: tag 0x34363248/'H264' is not supported with codec id 28 and format 'mp4 / MP4 (MPEG-4 Part 14)'
OpenCV: FFMPEG: fallback to use tag 0x00000021/'!???'

For codec=H264, extension=mp4
VLC media player 2.2.2 Weatherwax (revision 2.2.2-0-g6259d80)
[00000000015f8148] core libvlc: Running vlc with the default interface. Use 'cvlc' to use vlc without interface.
[00007f6994c01838] mp4 demux error: cannot find any /moov/trak
QObject::~QObject: Timers cannot be stopped from another thread
OpenCV: FFMPEG: tag 0x58564944/'DIVX' is not supported with codec id 13 and format 'mp4 / MP4 (MPEG-4 Part 14)'
OpenCV: FFMPEG: fallback to use tag 0x00000020/' ???'

For codec=DIVX, extension=mp4
VLC media player 2.2.2 Weatherwax (revision 2.2.2-0-g6259d80)
[0000000001880148] core libvlc: Running vlc with the default interface. Use 'cvlc' to use vlc without interface.
[00007f21acc017d8] mp4 demux error: cannot find any /moov/trak
QObject::~QObject: Timers cannot be stopped from another thread
OpenCV: FFMPEG: tag 0x44495658/'XVID' is not supported with codec id 13 and format 'mp4 / MP4 (MPEG-4 Part 14)'
OpenCV: FFMPEG: fallback to use tag 0x00000020/' ???'

For codec=XVID, extension=mp4
VLC media player 2.2.2 Weatherwax (revision 2.2.2-0-g6259d80)
[000000000178c148] core libvlc: Running vlc with the default interface. Use 'cvlc' to use vlc without interface.
[00007f1e64c01858] mp4 demux error: cannot find any /moov/trak
QObject::~QObject: Timers cannot be stopped from another thread
OpenCV: FFMPEG: tag 0x34363258/'X264' is not supported with codec id 28 and format 'mp4 / MP4 (MPEG-4 Part 14)'
OpenCV: FFMPEG: fallback to use tag 0x00000021/'!???'

For codec=X264, extension=mp4
VLC media player 2.2.2 Weatherwax (revision 2.2.2-0-g6259d80)
[0000000000baa148] core libvlc: Running vlc with the default interface. Use 'cvlc' to use vlc without interface.
[00007fdcc8c01838] mp4 demux error: cannot find any /moov/trak
QObject::~QObject: Timers cannot be stopped from another thread

我已经尝试了所有扩展名(.avi、.mp4)和编解码器(MJPG、DIVX、XVID、H264)的组合,但没有一个视频可以播放。

知道哪里出了问题吗?

最佳答案

讨论后https://github.com/ContinuumIO/anaconda-issues/issues/223 . 我采纳了 jeremy-rutman 的建议“我能够通过保持输出视频尺寸与输入帧尺寸相同来解决这个问题。”

以下代码对我有用,我使用 int(cap.get(3)) int(cap.get(4)) 作为帧大小。 (我的 api 也有点不同,比如 fourcc 检索)而且 waitKey 对我不起作用,所以我只循环到 100。

import cv2
import os

extensions = ['avi','mp4']
codecs = ['MJPG','H264', 'DIVX', 'XVID', 'X264']

for ext in extensions:
    for codec in codecs:
        savepath = "output__%s.%s" % (codec, ext)
        print savepath
        try:        
            cap = cv2.VideoCapture(0)

            # and our buffer to write frames
            fourcc = cv2.cv.CV_FOURCC(*codec)
            out = cv2.VideoWriter(savepath, fourcc, 20.0,     (int(cap.get(3)),int(cap.get(4))), 1)
            i = 0;
            while i < 100:
                i = i +1

                ret, frame = cap.read()
                if not ret:
                    continue

                text = 'testing 123'
                font = cv2.FONT_HERSHEY_SIMPLEX
                cv2.putText(frame, text, (50, 50), font, 2, (255, 255, 0), 2)

                #write our frame
                out.write(frame)

                cv2.imshow('My Frame', frame)
                ch = cv2.waitKey(1)
                if ch == 27:  # ESC
                    break

            cap.release()
            out.release()
            cv2.destroyAllWindows()

            os.system("vlc %s" % savepath)

    except Exception as e:
        print "Combo codec=%s, exte=%s failed with: %s" % (codec, ext, str(e))

关于python - 在 OpenCV 中保存带有文本叠加的视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45161869/

相关文章:

python - 自定义查找未在 Django 中注册

opencv - 当我减小图像尺寸时,为什么球体特征尺寸没有同时减小?

xml - OpenCv Haar函数创建的XML内部包含什么。

iphone - 在 iphone 的缩略图中显示视频时长

php - 使用正则表达式和 ffmpeg 获取视频的尺寸

python - 使用 Python 2 的套接字接口(interface)分别发送多个字符串

python - 通过迭代指定的数据帧值来创建新目录 Python Pandas Numpy

python - Scrapy:无法覆盖 __init__function

python - SIFT输入到ANN

css - 强制视频占用 div 的 100% 宽度