其實OpenCV已經(jīng)提供了寫視頻的接口,但是編碼和文件后綴的對應(yīng)真是讓人頭疼,動不動就不支持。經(jīng)過嘗試,總結(jié)出目前兩種可以正常寫入視頻的對應(yīng)關(guān)系:
- MJPG --> .avi
- mp4v -->.mp4
一、編碼MJPG、后綴.avi
MJPG是大寫!!!
# coding=utf-8
from
__future__
import
absolute_import
,
division
,
print_function
import
cv2
import
warnings
import
numpy
as
np
warnings
.
simplefilter
(
"always"
)
class
VideoWriter
:
def
__init__
(
self
,
name
,
width
,
height
,
fps
=
25
)
:
# type: (str, int, int, int) -> None
if
not
name
.
endswith
(
'.avi'
)
:
# 保證文件名的后綴是.avi
name
+=
'.avi'
warnings
.
warn
(
'video name should ends with ".avi"'
)
self
.
__name
=
name
# 文件名
self
.
__height
=
height
# 高
self
.
__width
=
width
# 寬
fourcc
=
cv2
.
VideoWriter_fourcc
(
*
'MJPG'
)
# 如果是avi視頻,編碼需要為MJPG
self
.
__writer
=
cv2
.
VideoWriter
(
name
,
fourcc
,
fps
,
(
width
,
height
)
)
def
write
(
self
,
frame
)
:
if
frame
.
dtype
!=
np
.
uint8
:
# 檢查frame的類型
raise
ValueError
(
'frame.dtype should be np.uint8'
)
# 檢查frame的大小
row
,
col
,
_
=
frame
.
shape
if
row
!=
self
.
__height
or
col
!=
self
.
__width
:
warnings
.
warn
(
'長和寬不等于創(chuàng)建視頻寫入時的設(shè)置,此frame不會被寫入視頻'
)
return
self
.
__writer
.
write
(
frame
)
def
close
(
self
)
:
self
.
__writer
.
release
(
)
def
main
(
)
:
width
=
512
height
=
256
vw
=
VideoWriter
(
'test.avi'
,
width
,
height
)
for
i
in
range
(
25
*
50
)
:
# 隨機(jī)生成一幅圖像
frame
=
np
.
random
.
randint
(
0
,
255
,
(
height
,
width
,
3
)
,
dtype
=
np
.
uint8
)
# 寫入圖像
vw
.
write
(
frame
)
# 關(guān)閉
vw
.
close
(
)
if
__name__
==
'__main__'
:
main
(
)
二、編碼mp4v、后綴.mp4
mp4v是小寫!!!
# coding=utf-8
from
__future__
import
absolute_import
,
division
,
print_function
import
cv2
import
warnings
import
numpy
as
np
warnings
.
simplefilter
(
"always"
)
class
VideoWriter
:
def
__init__
(
self
,
name
,
width
,
height
,
fps
=
25
)
:
# type: (str, int, int, int) -> None
if
not
name
.
endswith
(
'.mp4'
)
:
# 保證文件名的后綴是.mp4
name
+=
'.mp4'
warnings
.
warn
(
'video name should ends with ".mp4"'
)
self
.
__name
=
name
# 文件名
self
.
__height
=
height
# 高
self
.
__width
=
width
# 寬
fourcc
=
cv2
.
VideoWriter_fourcc
(
*
'mp4v'
)
# 如果是mp4視頻,編碼需要為mp4v
self
.
__writer
=
cv2
.
VideoWriter
(
name
,
fourcc
,
fps
,
(
width
,
height
)
)
def
write
(
self
,
frame
)
:
if
frame
.
dtype
!=
np
.
uint8
:
# 檢查frame的類型
raise
ValueError
(
'frame.dtype should be np.uint8'
)
# 檢查frame的大小
row
,
col
,
_
=
frame
.
shape
if
row
!=
self
.
__height
or
col
!=
self
.
__width
:
warnings
.
warn
(
'長和寬不等于創(chuàng)建視頻寫入時的設(shè)置,此frame不會被寫入視頻'
)
return
self
.
__writer
.
write
(
frame
)
def
close
(
self
)
:
self
.
__writer
.
release
(
)
def
main
(
)
:
width
=
512
height
=
256
vw
=
VideoWriter
(
'test.mp4'
,
width
,
height
)
for
i
in
range
(
25
*
50
)
:
# 隨機(jī)生成一幅圖像
frame
=
np
.
random
.
randint
(
0
,
255
,
(
height
,
width
,
3
)
,
dtype
=
np
.
uint8
)
# 寫入圖像
vw
.
write
(
frame
)
# 關(guān)閉
vw
.
close
(
)
if
__name__
==
'__main__'
:
main
(
)
三、視頻寫入失敗的原因
-
不支持對應(yīng)的編碼和后綴。目前我知道支持的是:
* mp4v–>.mp4
* MJPG–>.avi - frame的值類型不是uint8。
- frame的高或者寬和新建cv2.VideoWriter設(shè)置的高或?qū)挷粚?yīng)。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

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