Skip to content

Commit

Permalink
tools/opusmaker: add append time option and...
Browse files Browse the repository at this point in the history
...overwrite existing option
  • Loading branch information
DeadSix27 committed Jan 16, 2020
1 parent 4daa894 commit 08e5b5d
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion tools/opus_maker/opus.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,23 @@
# When batch-encoding a folder, every file with an extension not in this list will be ignored.
#
# Default: ('.mkv', '.mka', '.mp4', '.flac', '.m4a', '.mp3', '.aac', '.wav', '.oga', '.ogg', '.alac')
#
BATCH_EXTENSIONS = ('.mkv', '.mka', '.mp4', '.flac', '.m4a', '.mp3', '.aac', '.wav', '.oga', '.ogg', '.alac')

#
# If set to True, the script will overwrite the target files. If set to False it will append increasing numbers to the filenames if existing.
#
# Default: False
#
OVERWRITE_EXISTING = False

#
# Whether to append the selected time-region to the output filename. Example: Weeb_Music_0-40_0-24.flac
#
# Default: True
#
APPEND_TIME = True


# ########################### CODE ############################
# #############################################################
Expand Down Expand Up @@ -314,6 +329,8 @@ def __init__(self, input_path: Path,
ignore_mime: bool = None,
have_pyperclip: bool = None,
batch_extensions: bool = None,
overwrite_existing: bool = None,
append_time: bool = None,
) -> None:
self.extractCoverFromFile = self.convertCoverToJpg
errors = []
Expand All @@ -326,6 +343,8 @@ def __init__(self, input_path: Path,
self.withCover = with_cover
self.ignoreMime = ignore_mime
self.batchExtensions = batch_extensions
self.overwriteExisting = overwrite_existing
self.appendTime = append_time

# Commandline args
self.startTime = start_time
Expand Down Expand Up @@ -403,6 +422,26 @@ def __init__(self, input_path: Path,
for out_file in converted_files:
print(F"Moving output file to: {self.outputDir}")
new_output_file_path = self.outputDir.joinpath(out_file.name)

if self.appendTime:
if self.startTime:
new_output_file_path = new_output_file_path.append_stem(F"_{self.startTime.replace(':', '-')}")
if self.endTime:
new_output_file_path = new_output_file_path.append_stem("_")
if self.endTime:
if not self.startTime:
new_output_file_path = new_output_file_path.append_stem("0-00_")
new_output_file_path = new_output_file_path.append_stem(F"{self.endTime.replace(':', '-')}")

if not self.overwriteExisting: # re-think logic and combine with encodeFile()
if new_output_file_path.exists():
new_output_file_path_append = 1
while new_output_file_path.exists():
if new_output_file_path_append >= 10000:
raise Exception("Failed to find suitable alternative name for output file")
new_output_file_path = new_output_file_path.append_stem(F"_{new_output_file_path_append}")
new_output_file_path_append += 1

shutil.move(out_file, new_output_file_path)

if self.copyLink:
Expand Down Expand Up @@ -518,7 +557,9 @@ def append_stem(self, append_str: str) -> Path:
have_mime=HAVE_MIME,
ignore_mime=IGNORE_MIME,
have_pyperclip=HAVE_PYPERCLIP,
batch_extensions=BATCH_EXTENSIONS
batch_extensions=BATCH_EXTENSIONS,
overwrite_existing=OVERWRITE_EXISTING,
append_time=APPEND_TIME
)
else:
print("Syntax: opus <file> [<start_time> [<end_time]]")
Expand Down

0 comments on commit 08e5b5d

Please sign in to comment.