This is the 11th day of my participation in the August More Text Challenge. For details, see:August is more challenging

preface

Video placement B station: you can greatly watch by yourself!! Audio video: Video

Data source: Official website of League of Legends Development environment: Win10, Python3.7 Development tool: PyCharm

Image data collection

The crawler obtains all the skin data from the official website. The crawler is relatively simple and will not be introduced too much

Crawler analysis:

Get all hero ID data

Retrieve all hero image data based on the ID request hero detail JSON data

Send an asynchronous request to an image



#! /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): 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: 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() # Execute coroutine loop  __name__ == '__main__': crawl_image = Crawl_Image() crawl_image.run()Copy the code

The asynchronous addition of the crawler based on the coroutine will have a timeout exception, it is good to catch the timeout exception handling catch. Basically, you get these anomalies

asyncio.TimeoutError

aiohttp.client_exceptions.ServerDisconnectedError

aiohttp.client_exceptions.InvalidURL

aiohttp.client_exceptions.ClientConnectorError

Composite video

Synthesize all the images through 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' # frame rate FPS = 1 # VideoWriter_fourcc('m','p', '4.jpg', 'v') # videoMake = cv2.videowriter (video_dir, fourcc, FPS, 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 = Print (frame) frame = cv2.resize(frame, uint3) Videomake.write (frame) # Write into the video videomake.release () # Release resourcesCopy the code

Once done, the speed of the image can be adjusted according to the frame rate

Add sound to video

The final step is to add sound effects to the video. The addition of sound requires the FFmpeg tool which is available privately

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)
Copy the code

That’s it !!!!!

I am white and white I, an app girl who likes to share knowledge ❤️ [this post was written by my colleague Pak 汌 and posted to Nuggets with permission]

If you have no contact with the programming section of the friends see this blog, find do not understand or want to learn Python, you can directly leave a message + private I ducky [thank you very much for your likes, collection, attention, comments, one button four connect support]