QOL updates, removing redundant lines, making more compact

This commit is contained in:
LightArrowsEXE 2019-10-17 23:47:31 +02:00
parent bbaa8f8cc7
commit 060e87c5b1

View file

@ -1,15 +1,16 @@
#!/usr/bin/env python
"""
Generic script to generate keyframes for all files of a given extension using kagefunc's generate_keyframes function.
Generic script to generate keyframes for all files of a given extension
using kagefunc's generate_keyframes function.
Dependencies:
* VapourSynth
* wwxd (https://github.com/dubhater/vapoursynth-wwxd)
"""
import vapoursynth as vs
import glob
import argparse
import os
import vapoursynth as vs
from ast import literal_eval
core = vs.core
@ -21,10 +22,9 @@ def generate_keyframes(clip: vs.VideoNode, out_path=None, no_header=False) -> No
"""
clip = core.resize.Bilinear(clip, 640, 360, format=vs.YUV420P8) # speed up the analysis by resizing first
clip = core.wwxd.WWXD(clip)
if no_header:
out_txt = ''
else:
out_txt = "# WWXD log file, using qpfile format\n\n"
out_txt = '' if no_header else "# WWXD log file, using qpfile format\n# Please do not modify this file\n"
for i in range(clip.num_frames):
if clip.get_frame(i).props.Scenechange == 1:
out_txt += "%d I -1\n" % i
@ -37,38 +37,20 @@ def generate_keyframes(clip: vs.VideoNode, out_path=None, no_header=False) -> No
def main():
if args.file:
file_name = args.file
files = [file_name]
files = [args.file]
ext_in = os.path.splitext(files[0])[1]
else:
if args.recursive:
files = glob.glob('**/*', recursive=True)
else:
files = glob.glob('*')
files = glob.glob('*', recursive=args.recursive)
ext_in = args.extension if args.extension else "mkv"
if args.extension:
ext_in = args.extension
else:
ext_in = "mkv"
if args.noheader:
no_header = True
else:
no_header = False
if args.outfile:
out_file = args.outfile
else:
out_file = None
for f in files:
if f.endswith(ext_in):
print(f"\nGenerating keyframes for {f}:")
if f.endswith("m2ts"):
src = core.lsmas.LWLibavSource(f)
else:
src = core.ffms2.Source(f)
src = core.lsmas.LWLibavSource(f) if f.endswith("m2ts") else core.ffms2.Source(f)
print(f"Video info: {src.width}x{src.height}, {src.fps} fps, {src.format.name}")
if args.trims:
trims = literal_eval(args.trims)
@ -77,47 +59,55 @@ def main():
try:
src = core.std.Splice([src[slice(*trim)] for trim in trims])
except:
print("TypeError: Please make sure youre using a list for this function.\nFor example: -T \"[24,-24]\" , -T \"[None,30000],[None,-24]\", -T \"[None,16000],[16100,16200],[16300,None]\"")
print("TypeError: Please make sure youre using a list for this function.\nExample: -T \"[24,-24]\" , -T \"[None,30000],[None,-24]\", -T \"[None,16000],[16100,16200],[16300,None]\"")
return
if args.outfile:
generate_keyframes(src, os.path.join(os.path.dirname(f),out_file), no_header)
if args.outfile and args.file:
generate_keyframes(src, os.path.join(os.path.dirname(f),args.outfile), args.noheader)
else:
generate_keyframes(src, os.path.abspath(f"{f[:-len(ext_in)]}_keyframes.txt"), no_header)
generate_keyframes(src, os.path.abspath(f"{f[:-len(ext_in)]}_keyframes.txt"), args.noheader)
print(f"Progress: {src.num_frames}/{src.num_frames} frames")
if f.endswith(".m2ts"):
try:
os.remove(f"{f}.lwi")
except OSError:
except FileNotFoundError:
pass
else:
try:
os.remove(f"{f}.ffindex")
except OSError:
except FileNotFoundError:
pass
print(f"Progress: {src.num_frames}/{src.num_frames} frames")
if args.outfile:
print(f"Output: {out_file}\nDone.")
else:
print(f"Output: {f[:-len(ext_in)]}_keyframes.txt\nDone.")
else:
pass
print(f"Output: {args.outfile}\nDone.") if args.outfile and args.file else print(f"Output: {f[:-len(ext_in)]}_keyframes.txt\nDone.")
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-F", "--file",
help="specific file", action="store")
help="generate keyframes for a specific file",
action="store")
parser.add_argument("-R", "--recursive",
help="check recursively", action="store_true")
parser.add_argument("-E", "--extension", help="pick extension to generate keyframes for")
help="search files recursively",
action="store_true")
parser.add_argument("-E", "--extension",
default="mkv",
help="pick extension to generate keyframes for (default: %(default)s)",
action="store")
# TO-DO: Add mimetype recognition, add drag-and-drop functionality
# -E only exists so it automatically looks for a common video file type.
# If I can have it recognize video using mimetypes instead, there is no real need for this.
parser.add_argument("-N", "--noheader",
help="do not include header line for aegisub", action="store_true")
help="do not include header line for aegisub",
action="store_true")
parser.add_argument("-O", "--outfile",
default=None,
help="name for keyframes file output", action="store")
parser.add_argument("-T", "--trims",
help="string of trims to source file. format: \"[inclusive,exclusive],[inclusive,exclusive],[None,exclusive],[inclusive,None]\"", action="store")
help="string of trims to source file. " \
"format: \"[inclusive,exclusive],[inclusive,exclusive],[None,exclusive],[inclusive,None]\"",
action="store")
# TO-DO: Add a SCXvid mode for those where the WWXD format
# doesn't appear to work properly.
args = parser.parse_args()
main()