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站:各位大大可自行观看!!! 原声视频: 视频

  • 14
    点赞
  • 52
    收藏
    觉得还不错? 一键收藏
  • 9
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值