前言
数据来源: 英雄联盟官网
开发环境: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://blog.csdn.net/qq_45075118/article/details/115517973
查看评论