Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only delete audio files that need to be deleted #37

Merged
merged 2 commits into from
Sep 9, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions autosub/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,12 @@ def main():
base_directory = os.getcwd()
output_directory = os.path.join(base_directory, "output")
audio_directory = os.path.join(base_directory, "audio")
video_file_name = input_file.split(os.sep)[-1].split(".")[0]
audio_file_name = os.path.join(audio_directory, video_file_name + ".wav")
video_prefix = os.path.splitext(os.path.basename(input_file))[0]
audio_file_name = os.path.join(audio_directory, video_prefix + ".wav")

output_file_handle_dict = {}
for format in args.format:
output_filename = os.path.join(output_directory, video_file_name + "." + format)
output_filename = os.path.join(output_directory, video_prefix + "." + format)
print("Creating file: " + output_filename)
output_file_handle_dict[format] = open(output_filename, "w")
# For VTT format, write header
Expand All @@ -165,14 +165,15 @@ def main():

# Clean audio/ directory
for filename in os.listdir(audio_directory):
file_path = os.path.join(audio_directory, filename)
try:
if os.path.isfile(file_path) or os.path.islink(file_path):
os.unlink(file_path)
elif os.path.isdir(file_path):
shutil.rmtree(file_path)
except Exception as e:
print('Failed to delete %s. Reason: %s' % (file_path, e))
if filename.lower().endswith(".wav") and filename.startswith(video_prefix):
file_path = os.path.join(audio_directory, filename)
try:
if os.path.isfile(file_path) or os.path.islink(file_path):
os.unlink(file_path)
elif os.path.isdir(file_path):
shutil.rmtree(file_path)
except Exception as e:
print('Failed to delete %s. Reason: %s' % (file_path, e))

# Extract audio from input video file
extract_audio(input_file, audio_file_name)
Expand All @@ -182,11 +183,10 @@ def main():

print("\nRunning inference:")

for file in tqdm(sort_alphanumeric(os.listdir(audio_directory))):
audio_segment_path = os.path.join(audio_directory, file)

# Dont run inference on the original audio file
if audio_segment_path.split(os.sep)[-1] != audio_file_name.split(os.sep)[-1]:
for filename in tqdm(sort_alphanumeric(os.listdir(audio_directory))):
# Only run inference on relevant files, and don't run inference on the original audio file
if filename.startswith(video_prefix) and (filename != os.path.basename(audio_file_name)):
audio_segment_path = os.path.join(audio_directory, filename)
ds_process_audio(ds, audio_segment_path, output_file_handle_dict, split_duration=args.split_duration)

print("\n")
Expand Down