Add files via upload
This commit is contained in:
parent
9b29136535
commit
c89804bba5
74
autoditor.py
74
autoditor.py
@ -9,8 +9,8 @@ import argparse
|
|||||||
import os
|
import os
|
||||||
import tempfile
|
import tempfile
|
||||||
|
|
||||||
class Moment:
|
|
||||||
|
|
||||||
|
class Moment:
|
||||||
def __init__(self, start, stop):
|
def __init__(self, start, stop):
|
||||||
self.start = start
|
self.start = start
|
||||||
self.stop = stop
|
self.stop = stop
|
||||||
@ -33,7 +33,9 @@ def process_audio(source_audio_path: str) -> Tuple[np.ndarray, int, int]:
|
|||||||
return mono, duration, rate
|
return mono, duration, rate
|
||||||
|
|
||||||
|
|
||||||
def convert_video_to_audio(source_video_path: str, destination_audio_location = None) -> str:
|
def convert_video_to_audio(
|
||||||
|
source_video_path: str, destination_audio_location=None
|
||||||
|
) -> str:
|
||||||
tdir = tempfile.gettempdir()
|
tdir = tempfile.gettempdir()
|
||||||
dest_location = f"{tdir}/{source_video_path}.wav"
|
dest_location = f"{tdir}/{source_video_path}.wav"
|
||||||
print(f"checking to see if {dest_location} exists")
|
print(f"checking to see if {dest_location} exists")
|
||||||
@ -63,7 +65,7 @@ def sub_resample(data: np.ndarray, factor: int):
|
|||||||
|
|
||||||
|
|
||||||
def moving_average(x, w):
|
def moving_average(x, w):
|
||||||
return np.convolve(x, np.ones(w), 'valid') / w
|
return np.convolve(x, np.ones(w), "valid") / w
|
||||||
|
|
||||||
|
|
||||||
def find_highlights(data, threshold, rate, factor):
|
def find_highlights(data, threshold, rate, factor):
|
||||||
@ -78,10 +80,18 @@ def find_moving_average_highlights(short_ma, long_ma, bitrate, resample_factor):
|
|||||||
in_a_clip = False
|
in_a_clip = False
|
||||||
timestamps = []
|
timestamps = []
|
||||||
for t in range(1, len(long_ma)):
|
for t in range(1, len(long_ma)):
|
||||||
if not in_a_clip and (short_ma[t - 1] < long_ma[t - 1]) and (short_ma[t] > long_ma[t]):
|
if (
|
||||||
|
not in_a_clip
|
||||||
|
and (short_ma[t - 1] < long_ma[t - 1])
|
||||||
|
and (short_ma[t] > long_ma[t])
|
||||||
|
):
|
||||||
in_a_clip = True
|
in_a_clip = True
|
||||||
timestamps.append(t * resample_factor / bitrate)
|
timestamps.append(t * resample_factor / bitrate)
|
||||||
elif in_a_clip and (short_ma[t - 1] > long_ma[t - 1]) and (short_ma[t] < long_ma[t]):
|
elif (
|
||||||
|
in_a_clip
|
||||||
|
and (short_ma[t - 1] > long_ma[t - 1])
|
||||||
|
and (short_ma[t] < long_ma[t])
|
||||||
|
):
|
||||||
in_a_clip = False
|
in_a_clip = False
|
||||||
timestamps.append(t * resample_factor / bitrate)
|
timestamps.append(t * resample_factor / bitrate)
|
||||||
|
|
||||||
@ -103,7 +113,6 @@ def plot_audio(data):
|
|||||||
plt.show()
|
plt.show()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def main(vidfilepath, outfile, res_factor, lw, sw, dry_run, minduration, maxduration):
|
def main(vidfilepath, outfile, res_factor, lw, sw, dry_run, minduration, maxduration):
|
||||||
try:
|
try:
|
||||||
videofile = vidfilepath
|
videofile = vidfilepath
|
||||||
@ -116,15 +125,19 @@ def main(vidfilepath, outfile, res_factor, lw, sw, dry_run, minduration, maxdura
|
|||||||
LONG_WINDOW = lw
|
LONG_WINDOW = lw
|
||||||
SHORT_WINDOW = sw
|
SHORT_WINDOW = sw
|
||||||
|
|
||||||
assert(LONG_WINDOW > SHORT_WINDOW)
|
assert LONG_WINDOW > SHORT_WINDOW
|
||||||
|
|
||||||
long_ma = moving_average(squared_subsample, LONG_WINDOW)
|
long_ma = moving_average(squared_subsample, LONG_WINDOW)
|
||||||
short_ma = moving_average(squared_subsample, SHORT_WINDOW)
|
short_ma = moving_average(squared_subsample, SHORT_WINDOW)
|
||||||
moments = find_moving_average_highlights(short_ma, long_ma, bitrate, RESAMPLE_FACTOR)
|
moments = find_moving_average_highlights(
|
||||||
|
short_ma, long_ma, bitrate, RESAMPLE_FACTOR
|
||||||
|
)
|
||||||
total_time = 0
|
total_time = 0
|
||||||
for m in moments:
|
for m in moments:
|
||||||
if m.duration > minduration and m.duration < maxduration:
|
if m.duration > minduration and m.duration < maxduration:
|
||||||
print(f"Start {round(m.start/60, 2)} \t\t Stop {round(m.stop/60, 2)} \t\t Duration {round(m.duration, 2)}")
|
print(
|
||||||
|
f"Start {round(m.start/60, 2)} \t\t Stop {round(m.stop/60, 2)} \t\t Duration {round(m.duration, 2)}"
|
||||||
|
)
|
||||||
total_time = total_time + m.duration
|
total_time = total_time + m.duration
|
||||||
|
|
||||||
print(total_time / 60)
|
print(total_time / 60)
|
||||||
@ -141,15 +154,10 @@ def main(vidfilepath, outfile, res_factor, lw, sw, dry_run, minduration, maxdura
|
|||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
parser = argparse.ArgumentParser(
|
parser = argparse.ArgumentParser(
|
||||||
prog="autoditor",
|
prog="autoditor", description="autoditor is an automatic video editor."
|
||||||
description="autoditor is an automatic video editor."
|
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-v",
|
"-v", "--video", required=True, metavar="Video file path", dest="vpath"
|
||||||
"--video",
|
|
||||||
required=True,
|
|
||||||
metavar="Video file path",
|
|
||||||
dest="vpath"
|
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-f",
|
"-f",
|
||||||
@ -157,7 +165,7 @@ if __name__ == "__main__":
|
|||||||
default=16000,
|
default=16000,
|
||||||
metavar="Subsampling factor",
|
metavar="Subsampling factor",
|
||||||
dest="factor",
|
dest="factor",
|
||||||
type=int
|
type=int,
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-l",
|
"-l",
|
||||||
@ -165,7 +173,7 @@ if __name__ == "__main__":
|
|||||||
default=128,
|
default=128,
|
||||||
metavar="Long moving average time",
|
metavar="Long moving average time",
|
||||||
dest="lwindow",
|
dest="lwindow",
|
||||||
type=int
|
type=int,
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-s",
|
"-s",
|
||||||
@ -173,20 +181,11 @@ if __name__ == "__main__":
|
|||||||
default=64,
|
default=64,
|
||||||
metavar="Short moving average time",
|
metavar="Short moving average time",
|
||||||
dest="swindow",
|
dest="swindow",
|
||||||
type=int
|
type=int,
|
||||||
)
|
)
|
||||||
|
parser.add_argument("-d", "--dryrun", dest="drun", action="store_true")
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-d",
|
"-o", "--output", required=True, metavar="Output file location", dest="opath"
|
||||||
"--dryrun",
|
|
||||||
dest="drun",
|
|
||||||
action="store_true"
|
|
||||||
)
|
|
||||||
parser.add_argument(
|
|
||||||
"-o",
|
|
||||||
"--output",
|
|
||||||
required=True,
|
|
||||||
metavar="Output file location",
|
|
||||||
dest="opath"
|
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-i",
|
"-i",
|
||||||
@ -194,7 +193,7 @@ if __name__ == "__main__":
|
|||||||
default=30,
|
default=30,
|
||||||
metavar="Minimum clip duration",
|
metavar="Minimum clip duration",
|
||||||
dest="mindur",
|
dest="mindur",
|
||||||
type=int
|
type=int,
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-m",
|
"-m",
|
||||||
@ -202,9 +201,18 @@ if __name__ == "__main__":
|
|||||||
default=100,
|
default=100,
|
||||||
metavar="Maximum clip duration",
|
metavar="Maximum clip duration",
|
||||||
dest="maxdur",
|
dest="maxdur",
|
||||||
type=int
|
type=int,
|
||||||
)
|
)
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
# def main(vidfilepath, outfile, res_factor, lw, sw, dry_run):
|
# def main(vidfilepath, outfile, res_factor, lw, sw, dry_run):
|
||||||
main(args.vpath, args.opath, args.factor, args.lwindow, args.swindow, args.drun, args.mindur, args.maxdur)
|
main(
|
||||||
|
args.vpath,
|
||||||
|
args.opath,
|
||||||
|
args.factor,
|
||||||
|
args.lwindow,
|
||||||
|
args.swindow,
|
||||||
|
args.drun,
|
||||||
|
args.mindur,
|
||||||
|
args.maxdur,
|
||||||
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user