VaporwaveGenerator/main.py

186 lines
5.3 KiB
Python
Raw Normal View History

2020-03-10 15:15:47 +00:00
from src.VaporSong import VaporSong
from src.VHSImage import generateVHSStyle
2020-03-10 18:13:12 +00:00
from src.VHSVideo import VHS_Vid
2020-03-09 16:55:20 +00:00
import os
import youtube_dl
from logzero import logger
import re
import urllib.request
import urllib.parse
2020-03-09 17:20:22 +00:00
import argparse
2020-03-09 16:55:20 +00:00
2020-03-10 18:13:12 +00:00
version = 2.0
2020-03-09 17:20:22 +00:00
style = False
2020-07-07 15:55:32 +01:00
text = "| V A P O R W A V E || G E N E R A T O R |"
parser = argparse.ArgumentParser(description=text)
parser.add_argument(
"-M", "--music", help="generate v a p o r w a v e music", action="store_true"
)
parser.add_argument(
"-P", "--picture", help="generate VHS Style image", action="store_true"
)
parser.add_argument("-V", "--video", help="VHS Style Video", action="store_true")
2020-03-10 18:13:12 +00:00
parser.add_argument("-v", "--version", help="show program version", action="store_true")
2020-03-10 15:15:47 +00:00
parser.add_argument("-i", "--input")
2020-07-07 15:55:32 +01:00
parser.add_argument("-o", "--output", help="Output for specifying output video")
2020-03-10 15:15:47 +00:00
2020-03-09 17:20:22 +00:00
args = parser.parse_args()
2020-03-10 15:15:47 +00:00
music = False
picture = False
2020-03-10 18:13:12 +00:00
video = False
2020-03-10 15:15:47 +00:00
2020-03-09 17:20:22 +00:00
if args.version:
2020-07-07 15:55:32 +01:00
print("vaporwave generator 旺育栄", version)
exit
2020-03-09 17:20:22 +00:00
if args.music:
2020-07-07 15:55:32 +01:00
music = True
2020-03-10 15:15:47 +00:00
elif args.picture:
picture = True
2020-03-10 18:13:12 +00:00
elif args.video:
video = True
2020-03-09 17:20:22 +00:00
if args.input:
2020-07-07 15:55:32 +01:00
query = args.input
2020-03-10 18:13:12 +00:00
if args.output:
outfile = args.output
2020-03-09 17:20:22 +00:00
else:
2020-03-10 15:15:47 +00:00
parser.print_help()
exit
2020-03-09 17:20:22 +00:00
2020-07-07 15:55:32 +01:00
MAX_DURATION = 600 # In-case the program finds a compilation
youtube_urls = (
"youtube.com",
"https://www.youtube.com/",
"http://www.youtube.com/",
"http://youtu.be/",
"https://youtu.be/",
"youtu.be",
)
2020-03-09 16:55:20 +00:00
2020-07-07 15:55:32 +01:00
def download_file(query, request_id=1):
2020-03-09 16:55:20 +00:00
"""Returns audio to the vapor command handler
Searches YouTube for 'query', finds first match that has
duration under the limit, download video with youtube_dl
and extract .wav audio with ffmpeg.
Query can be YouTube link.
"""
ydl_opts = {
2020-07-07 15:55:32 +01:00
"quiet": "True",
"format": "bestaudio/best",
"outtmpl": str(request_id) + ".%(ext)s",
"prefer_ffmpeg": "True",
"noplaylist": "True",
"postprocessors": [
{
"key": "FFmpegExtractAudio",
"preferredcodec": "wav",
"preferredquality": "192",
}
],
2020-03-09 16:55:20 +00:00
}
original_path = str(request_id) + ".wav"
file_title = ""
# check if query is youtube url
if not query.lower().startswith((youtube_urls)):
# search for youtube videos matching query
2020-07-07 15:55:32 +01:00
query_string = urllib.parse.urlencode({"search_query": query})
html_content = urllib.request.urlopen(
"http://www.youtube.com/results?" + query_string
)
search_results = re.findall(
r"href=\"\/watch\?v=(.{11})", html_content.read().decode()
)
2020-03-09 16:55:20 +00:00
info = False
# find video that fits max duration
logger.info("Get video information...")
for url in search_results:
# check for video duration
try:
2020-07-07 15:55:32 +01:00
info = youtube_dl.YoutubeDL(ydl_opts).extract_info(url, download=False)
2020-03-09 16:55:20 +00:00
except Exception as e:
logger.error(e)
2020-07-07 15:55:32 +01:00
raise ValueError("Could not get information about video.")
full_title = info["title"]
if info["duration"] < MAX_DURATION and info["duration"] >= 5:
2020-03-09 16:55:20 +00:00
# get first video that fits the limit duration
logger.info("Got video: " + str(full_title))
2020-07-07 15:55:32 +01:00
file_title = info["title"]
2020-03-09 16:55:20 +00:00
break
2020-07-07 15:55:32 +01:00
2020-03-09 16:55:20 +00:00
# if we ran out of urls, return error
2020-07-07 15:55:32 +01:00
if not info:
raise ValueError("Could not find a video.")
2020-03-09 16:55:20 +00:00
# query was a youtube link
else:
logger.info("Query was a YouTube URL.")
url = query
2020-07-07 15:55:32 +01:00
info = youtube_dl.YoutubeDL(ydl_opts).extract_info(url, download=False)
file_title = info["title"]
2020-03-09 16:55:20 +00:00
# check if video fits limit duration
2020-07-07 15:55:32 +01:00
if info["duration"] < 5 or info["duration"] > MAX_DURATION:
raise ValueError("Video is too short. Need 5 seconds or more.")
2020-03-09 16:55:20 +00:00
# download video and extract audio
logger.info("Downloading video...")
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
try:
ydl.download([url])
except Exception as e:
logger.error(e)
2020-07-07 15:55:32 +01:00
raise ValueError("Could not download " + str(full_title) + ".")
2020-03-09 16:55:20 +00:00
return original_path, file_title
def gen_vapor(filePath, title):
2020-07-07 15:55:32 +01:00
# Delete stuff if there is anything left over.
os.system("rm -r download/")
os.system("rm -r beats/")
2020-03-09 16:55:20 +00:00
# Make the proper folders for intermediate steps
2020-07-07 15:55:32 +01:00
os.system("mkdir download/")
os.system("mkdir beats/")
2020-03-09 16:55:20 +00:00
2020-07-07 15:55:32 +01:00
# Download the youtube query's first result. Might be wrong but YOLO
# YTDownloader.download_wav_to_samp2(query)
2020-03-09 16:55:20 +00:00
2020-07-07 15:55:32 +01:00
# For every song in download folder(just one for now)
"""
2020-03-09 16:55:20 +00:00
for fs in os.listdir("download/"):
# Slow down the song.
VaporSong.vaporize_song(query,"download/"+fs)
pass
# When we are finished, delete the old folders.
"""
2020-07-07 15:55:32 +01:00
VaporSong.vaporize_song(filePath, title)
2020-03-09 16:55:20 +00:00
2020-07-07 15:55:32 +01:00
os.system("rm -r download/")
os.system("rm -r beats/")
2020-03-09 16:55:20 +00:00
2020-03-09 17:20:22 +00:00
"""
2020-03-09 16:55:20 +00:00
## Makes this a command line tool: disable when we get the webserver going
sys.argv.pop(0)
query = ""
for s in sys.argv:
query = query + s
2020-03-09 17:20:22 +00:00
"""
2020-03-09 16:55:20 +00:00
2020-03-09 17:20:22 +00:00
if music:
name, title = download_file(query)
gen_vapor(name, title)
2020-03-10 15:15:47 +00:00
elif picture:
2020-07-07 15:55:32 +01:00
generateVHSStyle(query, "out.jpg")
2020-03-10 18:13:12 +00:00
elif video:
VHS_Vid(query, outfile)