yt-pic-grabber/main.py

56 lines
1.6 KiB
Python

import sys
import os
import subprocess
import re
import scrapetube
# cleanup
for file in os.listdir('./out/'):
print('Cleanup ./out/' + file)
os.remove('./out/' + file)
#scraping
channel1_videos = scrapetube.get_channel("UCuefh-IW_h0HemxTUOA2PEA")
channel2_videos = scrapetube.get_channel("UC-pBFf-6YwMt3P5Lx6FbJbQ")
video_ids = [video.get('videoId') for video in channel1_videos] # online
video_ids += [video.get('videoId') for video in channel2_videos] # online
file_ids = [f.replace('_extra.png','') for f in os.listdir('/mnt/omv3/picture/YouTube Pics') if 'extra' in f] # already downloaded
new_vids = ['https://youtu.be/' + str(id) for id in video_ids if id not in file_ids]
# dl and processing
os.chdir('./out/')
for url in new_vids:
print('Downloading ' + url)
subprocess.call(["yt-dlp", "--write-thumbnail", "-f", "best", "--external-downloader", "ffmpeg", "--external-downloader-args", "ffmpeg_i:-ss 1 -t 11", url],
stdout=subprocess.DEVNULL,
stderr=subprocess.STDOUT)
for dl in os.listdir('.'):
print('Processing ' + dl)
match = re.search(r"\[([A-Za-z0-9_-]+)\]", dl)
#match = re.search(r"\[.*\]", dl)
if '.webp' in dl:
os.rename(dl, match.group(1) + '_extra.webp')
if '.mp4' in dl:
subprocess.call(["ffmpeg", "-sseof", "-3", "-i", dl, "-update", "1", "-q:v", "1", dl.replace('.mp4','') + ".jpg"],
stdout=subprocess.DEVNULL,
stderr=subprocess.STDOUT)
os.rename(dl.replace('.mp4','') + ".jpg", match.group(1) + '.jpg')
# cleanup
for file in os.listdir('.'):
if '[' in file:
print('Cleaning up ' + file)
os.remove(file)