python 通过视频url获取视频的宽高

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

这里其实是通过获取视频截图的方式获得大小的

下面列举两个小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('更新失败')

 

展开阅读全文

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