opencv-python 视频写入无法打开

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/u011599639/article/details/96861600

1. 问题

今天测试opencv文件的视频抓取和保存功能,测试代码如下,运行时视频可以正常播放,但是保存的output.mp4文件打不开(mac os),换了多个播放器都不行,仔细看才发现文件之后不到8k,也就是文件没有正确保存。尝试了安装ffmpeg,切换了fourcc文件中的配置,都不能解决问题。

import numpy as np
import cv2

cap = cv2.VideoCapture('M.AVI')

fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.mp4', fourcc, 20.0, (640, 480))

while (cap.isOpened()):
  ret, frame = cap.read()
  if ret == True:
    frame = cv2.flip(frame, 0)
    out.write(frame)
    cv2.imshow('frame', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
      break
  else:
    break

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

2.解决方案

​ 最终在知乎上找到了具体的原因(下面会有链接),当读取保存视频文件时要求尺寸必须保持一致,也就是要求读取的**‘M.AVI’视频文件和out中的后面的(640,480)保持一致。在这里不尺寸有问题,还需要更改尺寸中的文件’XVID’改为‘MJPG’**,更改后的代码如下:

import numpy as np
import cv2

cap = cv2.VideoCapture('M.AVI')

fourcc = cv2.VideoWriter_fourcc(*'MJPG')
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
print("width:",width, "height:", height)

out = cv2.VideoWriter('output.avi', fourcc, 20.0, (width, height))


while (cap.isOpened()):
  ret, frame = cap.read()
  if ret == True:
    frame = cv2.flip(frame, 0)
    out.write(frame)
    cv2.imshow('frame', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
      break
  else:
    break

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

参考:链接

展开阅读全文

A视频无法写入数据库

02-25

Asp代码无法传值,点击提交后是空白页。代码如下rnrn[code=html]<%rnrnaction = Trim(Request("action"))rnSelect Case actionrn Case "Main"rn Call Main()rn Case "saveEdit"rn Call saveEditData()rn Case "saveRecords"rn Call saveRecordsData()rn Case "saveHetong"rn Call saveHetongData()rn Case "editRecords"rn Call editRecordsData()rn rn Case Elsern Call main()rnEnd Selectrn%>rn[/code]rnrn[code=html] rn rn rn 合同编号: 合同状态:<% = getSelect("SelectData","Select_Hetong","hType") %> 产品:rn <%=L_Please_choose_01%>rn <% rn Set rsb = Conn.Execute("select * from ProductClass where pClassFid = '0' ")rn If Not rsb.Eof thenrn Do While Not rsb.Eofrn pClassid= rsb("pClassid")rn pClassname= rsb("pClassname")rn %>rn <%=pClassname%>rn <%rn rsb.Movenextrn Looprn End Ifrn rsb.Closern Set rsb = Nothing rn %>rn rn rn <%=L_Please_choose_02%>rn rn rn 吨数: 目的港: 付款方式: 价格: 总金额:<%=L_Yuan%>rn 出运日期: 预付款: 预付款日期: rn 尾款金额: 预计尾款到账期: 实际尾款到账期:rn 合同主体:rn <%=L_Please_choose_01%>rn 离岸公司rn 境内公司rn <%=L_Hetong_QDTime%>: 合同进展:rn 备注rn <%rnDim oFCKeditorrnSet oFCKeditor = New FCKeditorrnoFCKeditor.BasePath = "../FCKeditor/"rnoFCKeditor.ToolbarSet = "Default"rnoFCKeditor.Width = "100%"rnoFCKeditor.Height = "100"rnoFCKeditor.Value = hContentrnoFCKeditor.Create "hContent"rn%>rn <%=L_Hetong_Quanxian%>rn rn <%rn Set rs = Server.CreateObject("ADODB.Recordset")rn rs.Open "Select * From [user] Order By uId Asc ",conn,1,1 rn If rs.RecordCount = 0 Thenrn Response.Write("暂无会员")rn elsern Do While Not rs.BOF And Not rs.EOFrn %>rn " <%if rs("uLevel")=9 or rs("uName")=Session("CRM_name") then response.write ("checked onclick=""return false""")%>> <%=rs("uName")%>rn <%rn rs.MoveNextrn Looprn end ifrn rs.Closern Set rs = Nothingrn %>rn rn rn rn ">rn  rn rn rn rn [/code]rnrnSub代码如下rnrn[code=html]<%rnSub saveHetongData()rn rn Dim cIdrn Dim hBianhao,hType,hPBigclass,hPSmallclass,hNumber,hMudigang,hFukuanfangshi,hPrice,hMoney,hCYTime,hRevenuern Dim hYFTime,hWeikuan,hYJWKTime,hSJWKTime,hInvoice,hQDTime,HJinzhan,hContent,hPurview,hUserrn cId = CLng(ABS(Request("cId"))) rn hBianhao = Request.Form("hBianhao") rn hType = Request.Form("hType")rn hPBigclass = Request.Form("hPBigclass")rn hPSmallclass = Request.Form("Strade")rn hNumber = Request.Form("hNumber")rn hMudigang = Request.Form("hMudigang")rn hFukuanfangshi = Request.Form("hFukuanfangshi")rn hPrice = Request.Form("hPrice")rn hMoney = Request.Form("hMoney")rn hCYTime = Request.Form("hCYTime")rn hRevenue = Request.Form("hRevenue")rn hYFTime = Request.Form("hYFTime")rn hWeikuan = Request.Form("hWeikuan")rn hYJWKTime = Request.Form("hYJWKTime")rn hSJWKTime = Request.Form("hSJWKTime")rn hInvoice = Request.Form("hInvoice")rn hQDTime = Request.Form("hQDTime")rn HJinzhan = Request.Form("HJinzhan")rn hContent = RTrim(Request.Form("hContent"))rn hPurview = Request.Form("hPurview")rn hUser = Request.Form("hUser")rn If Not IsNumeric(cId) Or cId <= 0 Thenrn Response.Write(" ")rn Elsern Dim rsrn Set rs = Server.CreateObject("ADODB.Recordset")rn rs.Open "Select Top 1 * From Hetong",conn,3,2rn rn sql="UPDATE client SET cLastUpdated=' "&now()&" ' Where cId In (" & cId & ")"rn conn.execute sqlrn rn sql="UPDATE client SET cType='"&CRTypeEnd&"' Where cId In (" & cId & ")"rn conn.execute sqlrn rn rs.AddNewrn rs("cId") = cIdrn rs("hBianhao") = hBianhaorn rs("hType") = hTypern rs("hPBigclass") = hPBigclassrn rs("hPSmallclass") = hPSmallclassrn rs("hNumber") = hNumberrn rs("hMudigang") = hMudigangrn rs("hFukuanfangshi") = hFukuanfangshirn rs("hPrice") = hPricern rs("hMoney") = hMoneyrn rs("hCYTime") = hCYTimern rs("hRevenue") = hRevenuern rs("hYFTime") = hYFTimern rs("hWeikuan") = hWeikuanrn rs("hYJWKTime") = hYJWKTimern rs("hSJWKTime") = hSJWKTimern rs("hInvoice") = hInvoicern rs("hQDTime") = hQDTimern rs("HJinzhan") = HJinzhanrn rs("hContent") = hContentrn rs("Purview") = hPurviewrn rs("hUser") = hUserrn rn rs.Updatern rs.Closern Set rs = Nothing rn conn.execute ("insert into Logfile(lCid,lClass,lAction,lUser,lTime) values('"&cid&"','"&L_insert_class_04&"','"&L_insert_action_01&"','"&Session("CRM_name")&"','"&now()&"')") rn if ""&YNalert&"" = 1 thenrn Response.Write(" ")rn end ifrnrn Response.Write(" ") rn End IfrnEnd Subrn%>[/code] 论坛

没有更多推荐了,返回首页