1. 程式人生 > 程式設計 >python 通過視訊url獲取視訊的寬高方式

python 通過視訊url獲取視訊的寬高方式

這裡其實是通過獲取視訊截圖的方式獲得大小的

下面列舉兩個小demo

import cv2 #引入模組 獲取視訊截圖的
 
from PIL import Image #引入模組 獲取圖片大小
import os #引入系統命令 刪除圖片
video_full_path="http://qnmov.a.yximgs.com/upic/2018/06/06/12/BMjAxODA2MDYxMjQwMTZfMTkzMDUyMjRfNjU2NzMwNzI5MF8xXzM=_hd3_Bc143c8abf799984d2cc75a52de7039f0.mp4?tag=1-1530685096-h-0-xbkiau97pb-2b932528a435f1d0"
cap = cv2.VideoCapture(video_full_path)
#print(cap.isOpened())
if cap.isOpened():#正常開啟
  rval,frame = cap.read()
else:
  rval = False
cv2.imwrite("a.jpg",frame)
img = Image.open('a.jpg')
print(type(img.size))
print(img.size[0])
print(type(img.size[0]))
my_file = "images/1.jpg"
if os.path.exists(my_file):
  os.remove(my_file)
else:
 
  print("no such file")
 

demo2 更新資料庫

#!/usr/bin/env python3
#-*- coding:utf-8 -*-
from short_video import ShortVideo
import json
import re
import requests
import hashlib
import cv2
from PIL import Image
import logging
import os
try:
  query = ShortVideo().select()
except:
  print('操作失敗')
else:
  for item in query:
    shv = ShortVideo().select().where(ShortVideo.video_identify_md5 == item.video_identify_md5).get()
    video_url = shv.video_url
    cap = cv2.VideoCapture(video_url)
    if cap.isOpened():
      rval,frame = cap.read()
 
 
      cv2.imwrite("images/"+str(shv.id)+".jpg",frame)
      img = Image.open("images/"+str(shv.id)+".jpg")
      w = img.size[0]
      h = img.size[1]
      shv.width_height = str(w)+'x'+str(h)
      #更新完刪除檔案
      my_file = "images/"+str(shv.id)+".jpg"
      if os.path.exists(my_file):
        os.remove(my_file)
      else:
        logging.info("no such file")
    else:
      logging.info('id:'+str(shv.id)+"更新失敗")
      continue
 
 
    if shv.save() == 1:
      print('更新成功')
    else:
      print('更新失敗')
 

以上這篇python 通過視訊url獲取視訊的寬高方式就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援我們。