Convert pixiv to mp4 and download from pixiv using python's pixivpy

Introduction

Made by @choshicure Download pixiv's Ugoira There is a program called. It's very easy to use, but the gif is compressed quite a bit, and I can't move it cleanly. https://aviutl.info/gif-puraguinn/ The features of gif are described in detail here, so if you want to know, please have a look. I looked at PIL, but it seems that the degree of compression cannot be changed. So I decided to use openCV to make mp4 instead of gif. I also thought about ffmpeg, but I stopped it because it seems to be difficult to pass through the path.

Note

pixivpy is probably a library made by Chinese volunteers and is not official. Therefore, it is recommended to use a method such as throwing away the user id and password at the time of login so that they can be leaked. Abuse is strictly prohibited. There is an implicit rule of scraping that you can only requery once per second. Without this, it would be a dos attack and legally out. Please comment out sleep at your own risk. If you have any problems, please do so at your own risk.

environment

This program runs under windows10 environment. I don't know if it works under Linux or Mac environment. Works with the latest version of anaconda as of December 30, 2020

Library import

pip install opencv-python
pip install pixivpy

code

Up to the point of downloading the image Made by @choshicure [Download pixiv's mover] (https://qiita.com/choshicure/items/8795bf929e34af6622fc) Is the same as. Please read this article for explanations of variables.

The part to be converted to mp4 in the latter half Make a video from serial number images using Python3 + OpenCV I referred to.

ugoira_mp4_downloader.py


from pixivpy3 import *
from  time import sleep
from PIL import Image
import os, glob
import sys
import cv2

#Login process
aapi = AppPixivAPI()

ID = 'YOUR_ID'
PASS = 'YOUR_PASS'

aapi.login(ID, PASS)

#Waiting for illustration ID input
illust_id = int(input('>>'))

illust = aapi.illust_detail(illust_id)
ugoira_url = illust.illust.meta_single_page.original_image_url.rsplit('0', 1)
ugoira = aapi.ugoira_metadata(illust_id)
ugoira_frames = len(ugoira.ugoira_metadata.frames)
ugoira_delay = ugoira.ugoira_metadata.frames[0].delay
dir_name = str(illust_id)+'_ugoira'

#Create a folder to save the movement
if not os.path.isdir(dir_name):
    os.mkdir(dir_name)


#Download all images used in Ugoira
for frame in range(ugoira_frames):
    print(frame)
    frame_url = ugoira_url[0] + str(frame) + ugoira_url[1]
    aapi.download(frame_url, path=dir_name)
    sleep(2) #Wait 2 seconds after downloading



#Creating a video
frames = glob.glob(f'{dir_name}/*_ugoira*')
frames.sort(key=os.path.getmtime, reverse=False)

#parameter
fps = 1000 / ugoira_delay
height = illust.illust.height
width = illust.illust.width



# encoder(for mp4)
fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
# output file name, encoder, fps, size(fit to image size)
video = cv2.VideoWriter(f'{dir_name}/{illust_id}.mp4',fourcc, fps, (width, height))

for frame in frames:
    img = cv2.imread(frame)
    video.write(img)

video.release()
print('written')

Summary

The file size is smaller than gif, and you can now download beautiful mp4. However, mp4 cannot loop.

Download all works of a specific user from pixiv at once using pixivpy of python (including moving) I also want to support mp4

Reference site

[Download pixiv's Ugoira] (https://qiita.com/choshicure/items/8795bf929e34af6622fc) Make a video from serial number images using Python3 + OpenCV OpenCV video codec Make graph animation with Python (output mp4 file with OpenCV) cv::VideoWriter Class Reference Animate multiple still images with Python

Recommended Posts

Convert pixiv to mp4 and download from pixiv using python's pixivpy
How to convert Youtube to mp3 and download it super-safely [Python]
From Python to using MeCab (and CaboCha)
Use python's pixivpy to download all the works of a specific user from pixiv at once (including moving)
Use python's pixivpy to download all the works of the users you follow from pixiv at once (including moving) (Part 2)
Convert from Pandas DataFrame to System.Data.DataTable using Python for .NET
Convert DataFrame column names from Japanese to English using Googletrans
How to get followers and followers from python using the Mastodon API
Created a class to download and parse XBRL from UFO Catcher
[EC2] How to install and download chromedriver from the command line
Download images from "Irasutoya" using Scrapy
Export mp4 from maya using ffmpeg
Convert wma to mp3 on Mac
Convert from pdf to txt 2 [pyocr]
OpenMPI installation from download to pass-through
How to convert from .mgz to .nii.gz
I tried to convert datetime <-> string with tzinfo using strftime () and strptime ()
How to search using python's astroquery and get fits images with skyview
How to easily convert format from Markdown
Convert from PDF to CSV with pdfplumber
Download the file from S3 using boto.
Convert from katakana to vowel kana [python]
Porting and modifying doublet-solver from python2 to python3.
Easily download mp3 / mp4 with python and youtube-dl!
How to download youtube videos using pytube3
Scraping using lxml and saving to MySQL
Convert from Markdown to HTML in Python
Try to download Youtube videos using Pytube
Try to get a web page and JSON file using Python's Requests library