python英雄聯盟萬圖視頻制作
- 前言
- 圖片資料采集
- 圖片合成視頻
- 視頻添加音效
前言
資料來源: 英雄聯盟官網
開發環境:win10、python3.7
開發工具:pycharm
圖片資料采集
爬蟲獲取官網所有皮膚資料,爬蟲比較簡單不做過多介紹
爬蟲分析:
獲取所有英雄id資料
根據id請求英雄詳情json資料獲取所有英雄圖片資料
對圖片發送異步請求


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : BaiChuan
# @File : lol_skin_spider.py
import requests
import asyncio
import aiohttp
import time
class Crawl_Image:
def __init__(self):
self.url = 'https://game.gtimg.cn/images/lol/act/img/js/heroList/hero_list.js'
self.url1 = "https://game.gtimg.cn/images/lol/act/img/js/hero/{}.js"
self.path = r'E:\python_project\vip_course\lol_video\skins'
self.headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36',
}
async def get_image(self, url):
'''異步請求庫aiohttp 加快圖片 url 的網頁請求'''
async with aiohttp.ClientSession() as session:
response = await session.get(url)
content = await response.read()
return content
async def download_image(self, image):
html = await self.get_image(image[0])
with open(self.path + "\\" + image[1] + '.jpg', 'wb') as f:
f.write(html)
print('下載第{}張圖片成功'.format(image[1]))
def run(self):
hero_list = requests.get(self.url, headers=self.headers).json()
print(hero_list)
for hero in hero_list['hero']:
heroId = hero['heroId']
skins = requests.get(self.url1.format(heroId)).json()['skins']
task = [asyncio.ensure_future(self.download_image((skin['mainImg'], skin['name']))) for skin in skins]
loop = asyncio.get_event_loop()
# 執行協程
loop.run_until_complete(asyncio.wait(task))
if __name__ == '__main__':
crawl_image = Crawl_Image()
crawl_image.run()

該爬蟲基于協程的異步加入會有超時例外的報錯,直接捕獲就好了
超時例外處理
捕捉就好了…基本上碰到的有這些例外
asyncio.TimeoutError
aiohttp.client_exceptions.ServerDisconnectedError
aiohttp.client_exceptions.InvalidURL
aiohttp.client_exceptions.ClientConnectorError
圖片合成視頻
將全部的圖片通過cv2合成
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : BaiChuan
# @File : make_video.py
import cv2
import os
import numpy as np
video_dir = 'result.mp4'
# 幀率
fps = 1
# 圖片尺寸
img_size = (1920, 1080)
fourcc = cv2.VideoWriter_fourcc('m','p', '4.jpg', 'v') # 視頻編碼
videoMake = cv2.VideoWriter(video_dir, fourcc, fps, img_size) # 創建視頻
img_files = os.listdir(r'E:\python_project\vip_course\lol_video\skins')
for i in img_files:
img_path = r'E:\python_project\vip_course\lol_video\skins' + '\\' + i
print(img_path)
frame = cv2.imdecode(np.fromfile(img_path,dtype=np.uint8),-1) # 讀取進制資料
print(frame)
frame = cv2.resize(frame, img_size) # 生成視頻
videoMake.write(frame) # 寫進視頻里
videoMake.release() # 釋放資源

大功告成,圖片的播放速度可根據幀率調整
視頻添加音效
最后一步,給視頻配上音效
聲音的添加需要FFmpeg工具可在裙里獲取
資料獲取學習溝通裙:731685275
import subprocess
inmp4 = 'result.mp4'
inmp3 = 'Legends_Never_Die.mp3'
save_data = r'E:\python_project\vip_course\lol_video\data.mp4'
cmd=f'ffmpeg -i {inmp4} -i {inmp3} -codec copy ' + save_data
subprocess.call(cmd, shell=True)

大功告成!!!!!
視頻放置B站:各位大大可自行觀看!!! 原聲視頻: 視頻
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/274071.html
標籤:python
上一篇:自然語言處理-斗羅大陸詞切分
