【python】視頻、圖片使用request獲取及處理

視頻獲取

通過request

import requests
url = "https://flv.bn.netease.com/videolib3/1703/29/qozNg4588/SD/qozNg4588-mobile.mp4"
res = requests.get(url,stream=True)
with open('a.mp4','wb') as f:
    f.write(res.content)
f.close
print(res.status_code)
print(res)

視頻處理

將視頻切成圖片

使用pip install opencv-python

import cv2

#可以直接輸入url地址也可以先使用request請求存到本地讀取本地地址
mp4 = cv2.VideoCapture("https://flv.bn.netease.com/videolib3/1703/29/qozNg4588/SD/qozNg4588-mobile.mp4")
# mp4 = cv2.VideoCapture(a.mp4)
is_opened = mp4.isOpened()  # 判斷是否打開
print(is_opened)
fps = mp4.get(cv2.CAP_PROP_FPS)  # 獲取視頻的幀率
print(fps)
widght = mp4.get(cv2.CAP_PROP_FRAME_WIDTH)  # 獲取視頻的寬度
height = mp4.get(cv2.CAP_PROP_FRAME_HEIGHT)  # 獲取視頻的高度
print(str(widght) + "x" + str(height))
i = 0
count = 0
while is_opened:
    i += 1
    (flag, frame) = mp4.read()  # 讀取圖片
    if flag == True :
        if   i % num == 0:
            count = count+1
             file_name = "iamge" + str(count) + ".jpg"
            cv2.imwrite(file_name, frame, [cv2.IMWRITE_JPEG_QUALITY])  # 保存圖片  
        #對於JPEG,第三個參數表示的是圖像的質量,用0-100的整數表示,默認爲95。cv2.IMWRITE_JPEG_QUALITY類型爲Long,必須轉換成int
    else:
        break

圖片獲取

圖片轉成b64的方案

url路徑讀取

     pic_url = "https://yt-adp.nosdn.127.net/channel5/1080540_aigx_20180319.jpg"
     pic_res = requests.get(pic_url)#url路徑
     img = base64.b64encode(pic_res.content)
     print(img)

本地路徑讀取

  f = open(r'path', 'rb')#本地路徑
  img = base64.b64encode(f.read())
  print(img)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章