Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 129a948f02 | |||
| 34a99fdec0 | |||
| 15db01096f | |||
| 588fa17eb2 | |||
| b8326b9c73 | |||
| 93fe7d3fd7 | |||
| 5bb2e7cb0b | |||
| 98e4a79a4d | |||
| 811aceae81 |
+23
-15
@@ -164,6 +164,13 @@ def get_fragment_filename(phrase):
|
||||
s = s[:max_filename_length] + "..."
|
||||
return re.sub(r'(?u)[^-\w\'\.]', '', s)
|
||||
|
||||
def subprocess_call(args):
|
||||
try:
|
||||
output = subprocess.check_output(args, stderr=subprocess.STDOUT, universal_newlines=True)
|
||||
except subprocess.CalledProcessError as e:
|
||||
print("\n\n", e.output)
|
||||
sys.exit(1)
|
||||
|
||||
def create_fragments(search_phrase, clips, export_mode, output_dir):
|
||||
idx = 1
|
||||
|
||||
@@ -182,17 +189,15 @@ def create_fragments(search_phrase, clips, export_mode, output_dir):
|
||||
|
||||
t_fade = 0.2
|
||||
af = "afade=t=in:st=%s:d=%s,afade=t=out:st=%s:d=%s" % (0, t_fade, t - t_fade, t_fade)
|
||||
video_encoding_mode = "ultrafast"
|
||||
video_encoding_mode = "medium"
|
||||
|
||||
if export_mode["audio"]:
|
||||
cmd = " ".join(["ffmpeg", "-y", "-ss", str(ss), "-i", '"' + video_file + '"', "-loglevel", "quiet", "-t", str(t), "-af", af, '"' + fragment_filename + ".mp3" + '"'])
|
||||
p = subprocess.Popen(cmd)
|
||||
p.wait()
|
||||
cmd = ["ffmpeg", "-y", "-ss", str(ss), "-i", video_file, "-t", str(t), "-af", af, fragment_filename + ".mp3"]
|
||||
subprocess_call(cmd)
|
||||
|
||||
if export_mode["video"]:
|
||||
cmd = " ".join(["ffmpeg", "-y", "-ss", str(ss), "-i", '"' + video_file + '"', "-strict", "-2", "-loglevel", "quiet", "-t", str(t), "-map", "0", "-c:v", "libx264", "-preset", video_encoding_mode, "-c:a", "aac", "-ac", "2", "-af", af, '"' + fragment_filename + ".mp4" + '"'])
|
||||
p = subprocess.Popen(cmd)
|
||||
p.wait()
|
||||
cmd = ["ffmpeg", "-y", "-ss", str(ss), "-i", video_file, "-strict", "-2", "-t", str(t), "-map", "0:v", "-map", "0:a", "-c:v", "libx264", "-preset", video_encoding_mode, "-c:a", "aac", "-ac", "2", "-af", af, fragment_filename + ".mp4"]
|
||||
subprocess_call(cmd)
|
||||
|
||||
if export_mode["video-sub"]:
|
||||
srt_style = "FontName=Arial,FontSize=22"
|
||||
@@ -204,12 +209,11 @@ def create_fragments(search_phrase, clips, export_mode, output_dir):
|
||||
srt_filename = srt_filename.replace(",", "\\\\\\,")
|
||||
srt_filename = srt_filename.replace("'", "\\\\\\'")
|
||||
|
||||
vf = "\"" + "subtitles=" + srt_filename + ":force_style='" + srt_style + "',setpts=PTS-STARTPTS" + "\""
|
||||
vf = "subtitles=" + srt_filename + ":force_style='" + srt_style + "',setpts=PTS-STARTPTS"
|
||||
af = "afade=t=in:st=%s:d=%s,afade=t=out:st=%s:d=%s,asetpts=PTS-STARTPTS" % (ss, t_fade, to - t_fade, t_fade)
|
||||
|
||||
cmd = " ".join(["ffmpeg", "-y", "-ss", str(ss), "-i", '"' + video_file + '"', "-strict", "-2", "-loglevel", "quiet", "-t", str(t), "-map", "0", "-c:v", "libx264", "-preset", video_encoding_mode, "-c:a", "aac", "-ac", "2", "-vf", vf, "-af", af, "-copyts", '"' + fragment_filename + ".sub.mp4" + '"'])
|
||||
p = subprocess.Popen(cmd)
|
||||
p.wait()
|
||||
cmd = ["ffmpeg", "-y", "-ss", str(ss), "-i", video_file, "-strict", "-2", "-t", str(t), "-map", "0:v", "-map", "0:a", "-c:v", "libx264", "-preset", video_encoding_mode, "-c:a", "aac", "-ac", "2", "-vf", vf, "-af", af, "-copyts", fragment_filename + ".sub.mp4"]
|
||||
subprocess_call(cmd)
|
||||
|
||||
if export_mode["subtitles"]:
|
||||
subtitles_filename = video_file.rsplit('.', 1)[0] + ".srt"
|
||||
@@ -239,6 +243,7 @@ def play_clips(clips, ending_mode, mpv_options):
|
||||
with open(pipe_name, "wb", 0) as f_pipe:
|
||||
for clip_filename, clip_start, clip_end in clips:
|
||||
clip_filename = clip_filename.replace("\\","/")
|
||||
clip_filename = clip_filename.replace("\"", "\\\"")
|
||||
|
||||
cmd = ["loadfile", '"' + clip_filename + '"']
|
||||
if ending_mode:
|
||||
@@ -278,7 +283,7 @@ def main(media_dir, search_phrase, phrase_mode, phrases_gap, padding, limit, out
|
||||
|
||||
rg = shutil.which('rg')
|
||||
if rg:
|
||||
cmd = ["rg", "--no-heading", "--null-data", "-N", "-o", "-i", "-g", "*.txt", "-P", search_phrase_in_grep, media_dir]
|
||||
cmd = ["rg", "--sort-files", "--no-heading", "--null-data", "-N", "-o", "-i", "-g", "*.txt", "-P", search_phrase_in_grep, media_dir]
|
||||
else:
|
||||
cmd = ["grep", "-r", "-z", "-o", "-i", "--include", "*.txt", "-P", search_phrase_in_grep, media_dir]
|
||||
p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True, bufsize=-1)
|
||||
@@ -490,6 +495,8 @@ def parse_args(argv):
|
||||
if idx + 1 >= len(argv):
|
||||
return False
|
||||
args["output_dir"] = os.path.abspath(argv[idx + 1])
|
||||
if not os.path.exists(args["output_dir"]):
|
||||
os.makedirs(args["output_dir"])
|
||||
idx += 1
|
||||
elif argv[idx] == "--ending" or argv[idx] == "-e":
|
||||
args["ending_mode"] = True
|
||||
@@ -533,9 +540,10 @@ def validate_args(args):
|
||||
if not os.path.isdir(args["media_dir"]):
|
||||
print("ERROR: '{}' is not a folder".format(args["media_dir"]))
|
||||
return False
|
||||
if not os.path.isdir(args["output_dir"]):
|
||||
print("ERROR: '{}' is not a folder".format(args["output_dir"]))
|
||||
return False
|
||||
if args["output_dir"]:
|
||||
if os.path.exists(args["output_dir"]) and not os.path.isdir(args["output_dir"]):
|
||||
print("ERROR: '{}' is not a folder".format(args["output_dir"]))
|
||||
return False
|
||||
if args["grep_file"]:
|
||||
if os.path.isdir(args["grep_file"]):
|
||||
print("ERROR: '{}' can't be a folder".format(args["grep_file"]))
|
||||
|
||||
Reference in New Issue
Block a user