Skip to content

Commit

Permalink
small feature update
Browse files Browse the repository at this point in the history
* enabled separate control over aivd and ffmpeg thread count
* added option to pass custom arguments to ffmpeg
  • Loading branch information
Davis-Software committed Jan 19, 2024
1 parent 3faf30e commit 47466bd
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 30 deletions.
36 changes: 19 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,25 @@ Thus, it can for example be used to find the intro of a tv show in its episodes.
DIRECTORY: The directory with the video or audio files to search in.
```

| Option | Data Type | Description | Default |
|---------------------|----------------------|-------------------------------------------------------------------------------------|------------------------------------------------|
| `-r`, `--recursive` | flag | Search recursively in the specified directory. | |
| `-e`, `--extension` | `string` | The extension of the video/audio files to search in. Can be a comma separated list. | `mp4,mkv,avi,mov,wmv,mp3,wav,flac,ogg,m4a,wma` |
| `-x`, `--exclude` | `string` | Exclude the specified extension from the search. Can be a comma separated list. | `""` |
| `-t`, `--time` | `integer` | How many seconds of the input audio file to search for. | `-1` (meaning the entire file) |
| `-w`, `--window` | `integer` | The window size in seconds to search for the audio file. | `60` |
| `-f`, `--format` | `json \| txt \| raw` | The output format. | `"txt"` |
| `-c`, `--threads` | `integer` | The number of CPU threads to use. | half of system cpu threads |
| `--ffmpeg` | `string` | The path to the ffmpeg executable. | from system path |
| `--no-clean` | flag | Do not clean up temporary files. | |
| `--silent` | flag | Do not print anything but the final output to the console. | |
| `--debug` | flag | Print debug information to the console. | |
| `--dry-run` | flag | Do not run the program, just print the parameters. | |
| `--version` | flag | Print the version number and exit. | |
| `--legacy` | flag | Use the legacy cli. | |
| `--help` | flag | Show help message and exit. | |
| Option | Data Type | Description | Default |
|----------------------|----------------------|-------------------------------------------------------------------------------------|--------------------------------------------------|
| `-r`, `--recursive` | flag | Search recursively in the specified directory. | |
| `-e`, `--extension` | `string` | The extension of the video/audio files to search in. Can be a comma separated list. | `"mp4,mkv,avi,mov,wmv,mp3,wav,flac,ogg,m4a,wma"` |
| `-x`, `--exclude` | `string` | Exclude the specified extension from the search. Can be a comma separated list. | `""` |
| `-t`, `--time` | `integer` | How many seconds of the input audio file to search for. | `-1` (meaning the entire file) |
| `-w`, `--window` | `integer` | The window size in seconds to search for the audio file. | `60` |
| `-f`, `--format` | `json \| txt \| raw` | The output format. | `"txt"` |
| `-c`, `--threads` | `integer` | The number of CPU threads to use. | half of system cpu threads |
| `--ffmpeg` | `string` | The path to the ffmpeg executable. | from system path |
| `--ffmpeg-processes` | `integer` | The number of ffmpeg processes to run at the same time. | `1` |
| `--ffmpeg-args` | `string` | Additional arguments to pass to ffmpeg. Best pass them in quotes. | `None` |
| `--no-clean` | flag | Do not clean up temporary files. | |
| `--silent` | flag | Do not print anything but the final output to the console. | |
| `--debug` | flag | Print debug information to the console. | |
| `--dry-run` | flag | Do not run the program, just print the parameters. | |
| `--version` | flag | Print the version number and exit. | |
| `--legacy` | flag | Use the legacy cli. | |
| `--help` | flag | Show help message and exit. | |

### Legacy CLI
```shell
Expand Down
19 changes: 11 additions & 8 deletions detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def _detector_thread(file_obj):

class Detector:
def __init__(self, base_file: str, files: list[str], time_: int, window: int, ffmpeg: str, logger: Logger,
clean=True, threads=1):
clean=True, aivd_threads=1, ffmpeg_processes=1):
self._base_file = base_file
self._files = files
self.__ready_files, self.__to_convert = os_helpers.is_audio_files(files)
Expand All @@ -48,8 +48,8 @@ def __init__(self, base_file: str, files: list[str], time_: int, window: int, ff
self.__correlate = None
self.__argmax = None

self._ffmpeg_semaphore = threading.Semaphore(threads)
self._threads = threads
self._ffmpeg_semaphore = threading.Semaphore(ffmpeg_processes)
self._threads = aivd_threads

self.time = time_
self.window = window
Expand All @@ -74,7 +74,7 @@ def __exit__(self, exc_type, exc_val, exc_tb):
self.logger.error(f"The Application was terminated: {exc_type.__name__}")
self.clean_up()

def __ffmpeg_thread(self, file, file_obj):
def __ffmpeg_thread(self, file, file_obj, additional_args=None):
self._ffmpeg_semaphore.acquire()
target = file_obj["location"]
self.logger.debug(f"\tConverting '{file}' to '{target}'...")
Expand All @@ -90,6 +90,9 @@ def __ffmpeg_thread(self, file, file_obj):
target
])

if additional_args is not None:
ffmpeg_opts.extend(additional_args)

ffmpeg = subprocess.Popen(
ffmpeg_opts,
stdout=subprocess.PIPE,
Expand Down Expand Up @@ -136,7 +139,7 @@ def _load(self):
self.logger.debug("Base file loaded.")
self.logger.empty_line()

def _convert(self):
def _convert(self, ffmpeg_args=None):
if len(self.__to_convert) == 0:
self.logger.info("No files to convert. Skipping conversion...")
return
Expand All @@ -152,7 +155,7 @@ def _convert(self):

threading.Thread(
target=self.__ffmpeg_thread,
args=(file, file_obj, )
args=(file, file_obj, ffmpeg_args, )
).start()

self.__converter_map[file] = file_obj
Expand Down Expand Up @@ -209,9 +212,9 @@ def clean_up(self):
self.logger.debug("Clean up complete.")
self.logger.empty_line()

def run(self):
def run(self, ffmpeg_args=None):
self._load()
self._convert()
self._convert(ffmpeg_args)
self._detect()

return self._output_data
19 changes: 14 additions & 5 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from utils import os_helpers
from utils.logger import Logger

__version__ = "2.1.3"
__version__ = "2.1.4"

_PERMITTED_EXTENSIONS = ["mp4", "mkv", "avi", "mov", "wmv", "mp3", "wav", "flac", "ogg", "m4a", "wma"]

Expand Down Expand Up @@ -49,6 +49,10 @@ def load_legacy(ctx, _param, value):
"both the audio file conversion via ffmpeg and the audio file search.")
@click.option("--ffmpeg", type=click.Path(exists=True), default=lambda: os_helpers.find_ffmpeg(),
help="The path to the ffmpeg executable. Default is the system path.")
@click.option("--ffmpeg-processes", type=int, default=1, help="The number of ffmpeg processes to run at the same time."
"Default is 1.")
@click.option("--ffmpeg-args", type=str, default=None, help="Additional arguments to pass to ffmpeg."
"Best pass them in quotes.")
@click.option("--no-clean", is_flag=True, help="Do not clean up temporary files.")
@click.option("--silent", is_flag=True, help="Do not print anything but the final output to the console.")
@click.option("--debug", is_flag=True, help="Print debug information to the console.")
Expand All @@ -57,8 +61,8 @@ def load_legacy(ctx, _param, value):
expose_value=False, is_eager=True)
@click.option("--legacy", is_flag=True, help="Use the legacy cli.", callback=load_legacy,
expose_value=False, is_eager=True)
def main(input_file, directory, recursive, extension, exclude, time_, window, format_, threads, ffmpeg, no_clean,
silent, debug, dry_run):
def main(input_file, directory, recursive, extension, exclude, time_, window, format_, threads, ffmpeg,
ffmpeg_processes, ffmpeg_args, no_clean, silent, debug, dry_run):
"""
Find the INPUT_FILE audio file in the specified video or audio files in a folder and return the time index.
Expand Down Expand Up @@ -92,6 +96,8 @@ def main(input_file, directory, recursive, extension, exclude, time_, window, fo
logger.debug(f"\tFormat: {format_}")
logger.debug(f"\tThreads: {threads}")
logger.debug(f"\tFFmpeg: '{ffmpeg}'")
logger.debug(f"\tFFmpeg processes: {ffmpeg_processes}")
logger.debug(f"\tFFmpeg args: {ffmpeg_args}")
logger.debug(f"\tSkipping clean up: {no_clean}")
logger.empty_line()

Expand All @@ -115,8 +121,11 @@ def main(input_file, directory, recursive, extension, exclude, time_, window, fo
logger.info("Dry run, exiting!")
return

with Detector(input_file, files, time_, window, ffmpeg, logger, not no_clean, threads) as detector:
data = detector.run()
with Detector(input_file, files, time_, window, ffmpeg, logger, not no_clean, threads, ffmpeg_processes)\
as detector:
data = detector.run(
ffmpeg_args if (ffmpeg_args is not None and ffmpeg_args != "" and ffmpeg_args != "None") else None
)

if format_ == "json":
logger.debug("Outputting JSON...")
Expand Down

0 comments on commit 47466bd

Please sign in to comment.