From 57733d602b1c5d59ab4e516be18aecf4f4b4627e Mon Sep 17 00:00:00 2001 From: K4YT3X Date: Sat, 24 Feb 2018 16:13:27 -0500 Subject: [PATCH] basic functions online --- __pycache__/ffmpeg.cpython-36.pyc | Bin 1484 -> 0 bytes ffmpeg.py | 15 +++--- video2x.py | 77 ++++++++++++++++++++++++++---- waifu2x.py | 8 ++-- 4 files changed, 81 insertions(+), 19 deletions(-) delete mode 100644 __pycache__/ffmpeg.cpython-36.pyc diff --git a/__pycache__/ffmpeg.cpython-36.pyc b/__pycache__/ffmpeg.cpython-36.pyc deleted file mode 100644 index 46c0b4a106e89bc4f7b4abeb8d1999e69244a7ea..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1484 zcmb7EL2uhO6qaPicGS29+5#H}tl*^w1C^B6UNHp2kRV-qSZf4mF|+~-LKUfmnIvi? zZP;0MO8>xq#jx!k>Dtr&!cKdSvJ$7=r4;x`kx!5B`@Tm_hC~0azkiS4b{yw}v(ez) zIe@MofMHJTFqe6`8@taO)?wZE4(ncevB$iZ&S>lW4}X>ve4jjh`r`P9XXGePwDu3@ z=2WVE^5gW)tI2EsNn$uTQXB>bp8OpdPal!-?%ucl^F$l+LNbwwPxkyLT&GG@M#yrX zygC({q!^x@CMC3xij={xTEohE~@-AnB{Y~hG!*;McgWh*DNh%pnNh+%qiA+(+Xl|;x36ePzGE9xQ*tB*P98A(CzuK`s{#&AtWvvcD%U>RB# zZeOvmiT$v}E{H9mFj)w!#73^7ngFnCK?4o{6~p15u|@^nAm5dqBi91@IOAajsI({> z70E{8t%^4sW}^Y#wGwKU(%PrAkZhjge23Cs=1Jb{s2&7Tx6tgM8KOay8^_yn!a`8U r!(Hea%WU8cI?ZF(;coRQWiq9-!Gg~J7o{G2*W~g&9%g?qT?q9bkHK9F diff --git a/ffmpeg.py b/ffmpeg.py index 8a9e3cd..a3c4d55 100644 --- a/ffmpeg.py +++ b/ffmpeg.py @@ -17,17 +17,18 @@ import os class FFMPEG: - def __init__(self): - pass + def __init__(self, ffmpeg_path, outfile): + self.ffmpeg_path = ffmpeg_path + self.outfile = outfile def strip_frames(self, videoin, outpath): - os.system("ffmpeg -i {} -r 1/1 {}/extracted_%0d.png".format(videoin, outpath)) + os.system("{} -i {} {}/extracted_%0d.png -y".format(self.ffmpeg_path, videoin, outpath)) def extract_audio(self, videoin, outpath): - os.system("ffmpeg -i {} -vn -acodec copy {}/output-audio.aac".format(videoin, outpath)) + os.system("{} -i {} -vn -acodec copy {}/output-audio.aac -y".format(self.ffmpeg_path, videoin, outpath)) def to_vid(self, framerate, resolution, folder): - os.system("ffmpeg -r {} -f image2 -s {} -i {}/extracted_%d.png -vcodec libx264 -crf 25 -pix_fmt yuv420p output.mp4".format(framerate, resolution, folder)) + os.system("{} -r {} -f image2 -s {} -i {}/extracted_%d.png -vcodec libx264 -crf 25 -pix_fmt yuv420p output.mp4 -y".format(self.ffmpeg_path, framerate, resolution, folder)) - def pressin_audio(self, videoin): - os.system("ffmpeg -i {} -i audio.mp3 -codec copy -shortest output.mp4") + def pressin_audio(self, videoin, outpath): + os.system("{} -i {} -i {}/output-audio.aac -codec copy -shortest {} -y".format(self.ffmpeg_path, videoin, outpath, self.outfile)) diff --git a/video2x.py b/video2x.py index 82d7cdb..0fcc2a8 100644 --- a/video2x.py +++ b/video2x.py @@ -12,15 +12,76 @@ Version 1.0 """ from ffmpeg import FFMPEG +from fractions import Fraction from waifu2x import WAIFU2X +import argparse +import json import os -fm = FFMPEG() -w2 = WAIFU2X() -if not os.path.isdir("frames"): - os.mkdir("frames") -fm.strip_frames("testf.mp4", "frames") +FFMPEG_PATH = "C:/Program Files (x86)/ffmpeg/bin/" +WAIFU2X_PATH = "\"C:/Program Files (x86)/waifu2x-caffe/waifu2x-caffe-cui.exe\"" -if not os.path.isdir("upscaled"): - os.mkdir("upscaled") -w2.upscale("frames", "upscaled") +FOLDERIN = "frames" +FOLDEROUT = "upscaled" + + +def processArguments(): + """This function parses all arguments + This allows users to customize options + for the output video. + """ + global args + parser = argparse.ArgumentParser() + control_group = parser.add_argument_group('Controls') + control_group.add_argument("-f", "--factor", help="Factor to enlarge video by", action="store", default=2) + control_group.add_argument("-v", "--video", help="Specify video file", action="store", default=False) + control_group.add_argument("-o", "--output", help="Specify output file", action="store", default=False) + args = parser.parse_args() + + +def get_vid_info(): + """Gets original video information + This function uses ffprobe to determine the + properties of the original video. + + It returns a dict + """ + os.system("{} -v quiet -print_format json -show_format -show_streams {} > info.json".format("\"" + FFMPEG_PATH + "ffprobe.exe\"", args.video)) + json_file = open('info.json', 'r') + json_str = json_file.read() + print(json.loads(json_str)) + return json.loads(json_str) + + +def main(): + """Main flow control function for video2x. + This function takes care of the order of operation. + """ + fm = FFMPEG("\"" + FFMPEG_PATH + "ffmpeg.exe\"", args.output) + w2 = WAIFU2X(WAIFU2X_PATH) + + # Extract Frames + if not os.path.isdir(FOLDERIN): + os.mkdir(FOLDERIN) + fm.strip_frames(args.video, FOLDERIN) + + info = get_vid_info() + width, height, framerate = info["streams"][0]["width"], info["streams"][0]["height"], float(Fraction(info["streams"][0]["avg_frame_rate"])) + print("Framerate: ", framerate) + final_resolution = str(width * int(args.factor)) + "x" + str(height * int(args.factor)) + + # Upscale Frames + if not os.path.isdir(FOLDEROUT): + os.mkdir(FOLDEROUT) + w2.upscale(FOLDERIN, FOLDEROUT, int(args.factor) * width, int(args.factor) * height) + + # Frames to Video + fm.to_vid(framerate, final_resolution, FOLDEROUT) + + # Extract and press audio in + fm.extract_audio(args.video, FOLDEROUT) + fm.pressin_audio("output.mp4", FOLDEROUT) + + +processArguments() +main() diff --git a/waifu2x.py b/waifu2x.py index 2cc5246..60753d8 100644 --- a/waifu2x.py +++ b/waifu2x.py @@ -17,8 +17,8 @@ import os class WAIFU2X: - def __init__(self): - pass + def __init__(self, waifu2x_path): + self.waifu2x_path = waifu2x_path - def upscale(self, factor, folderin, folderout): - os.system("waifu2x-caffe-cui.exe -i {} -o {}".format(folderin, folderout)) + def upscale(self, folderin, folderout, width, height): + os.system("{} -p cpu -I png -i {} -e png -o {} -w {} -h {}".format(self.waifu2x_path, folderin, folderout, width, height))