From 15350d5b2ae1f03afb4b682e411c87ef7ed8e646 Mon Sep 17 00:00:00 2001 From: Daniel Roy Greenfeld <62857+pydanny@users.noreply.github.com> Date: Sun, 29 Oct 2023 10:22:48 +0100 Subject: [PATCH 1/2] Improvements to slicer --- src/interviewkit/slicer.py | 117 ++++++++++++++++++------------------- 1 file changed, 58 insertions(+), 59 deletions(-) diff --git a/src/interviewkit/slicer.py b/src/interviewkit/slicer.py index 785747e..8f60a4c 100644 --- a/src/interviewkit/slicer.py +++ b/src/interviewkit/slicer.py @@ -8,10 +8,10 @@ `ffmpeg` is a requirement for this to work. Example usage: - Format: python interviewkit/slicer.py path_to_audio_file audio_slice_start_time audio_slice_end_time - Example 1: python interviewkit/slicer.py data/MartineBarrat_FINAL.mp3 2 3 + Format: hist slice path_to_audio_file audio_slice_start_time audio_slice_end_time + Example 1: hist slice data/MartineBarrat_FINAL.mp3 2 3 (Audio will be sliced from 2mins to 4mins) - Example 2: python interviewkit/slicer.py data/Martine+Barrat_FINAL.mp3 70:40 80:40 + Example 2: hist slice data/Martine+Barrat_FINAL.mp3 70:40 80:40 (Audio will be sliced from 105mins 40secs to 107mins and 40secs) This generates: @@ -28,36 +28,39 @@ """ import shutil -import sys from pathlib import Path +import pydub +from rich.console import Console + + +# pretty messaging +console = Console() +def error(msg): + """Print messages using red text""" + console.print(msg, "bold red") -try: - import pydub -except ImportError: - print("Please install pydub: pip install pydub") - exit(1) if shutil.which("ffmpeg") is None: - print("Please install ffmpeg: https://ffmpeg.org/download.html") - print(" On mac you can: brew install ffmpeg") + error("Please install ffmpeg: https://ffmpeg.org/download.html") + error(" On mac you can: brew install ffmpeg") exit(1) def convert_audio_time_to_msec(audio_time_split_list): """ Converting mins and secs to msecs for pydub computation """ if(audio_time_split_list): - if(len(audio_time_split_list) == 1): + if (len(audio_time_split_list) == 1): return int(audio_time_split_list[0]) * 60 * 1000 elif(len(audio_time_split_list) == 2): return int(audio_time_split_list[0]) * 60 * 1000 + int(audio_time_split_list[1]) * 1000 else: - print("Error! Audio slice input params invalid. Audio slice supports start/end time in mins or mins:secs format. Please try again with correct input times.") - print("Error inside convert_audio_time_to_msec(audio_time_split_list) funtion.") + error("Error! Audio slice input params invalid. Audio slice supports start/end time in mins or mins:secs format. Please try again with correct input times.") + error("Error inside convert_audio_time_to_msec(audio_time_split_list) funtion.") exit(1) else: - print("Error! Audio slice input params invalid. Please try again with correct parameters.") - print("Error inside convert_audio_time_to_msec(audio_time_split_list) funtion.") + error("Error! Audio slice input params invalid. Please try again with correct parameters.") + error("Error inside convert_audio_time_to_msec(audio_time_split_list) funtion.") exit(1) def export_filename(audio_time_list): @@ -68,53 +71,49 @@ def export_filename(audio_time_list): elif audio_time_list and len(audio_time_list) == 1: return f"{audio_time_list[0]}m" else: - print("Error! Audio slice input params invalid. Please try again with correct parameters.") - print("Error inside export_filename(audio_time_list) funtion.") + error("Error! Audio slice input params invalid. Please try again with correct parameters.") + error("Error inside export_filename(audio_time_list) funtion.") exit(1) -def audio_slicing(path, audio_slice_start_time, audio_slice_end_time): +def audio_slicing(path: Path, audio_slice_start_time: str, audio_slice_end_time: str): """ It reads the original audio and uses start and end input time params to generate sliced audio. """ - print("Sampling {} from {} to {}".format(path, audio_slice_start_time, audio_slice_end_time)) - - # Reading original audio file - audio = pydub.AudioSegment.from_file(path) - original_audio_size_ms = audio.duration_seconds * 1000 - - # Fetching mins and secs from audio input - audio_start_time_list = audio_slice_start_time.split(":") - audio_end_time_list = audio_slice_end_time.split(":") + with console.status( + f"[bold]Slicing {path}...[/bold]", spinner="bouncingBar" + ): + pretty_filename = f"{path.parent.name}/{path.name}" + console.log(f"Sampling {pretty_filename}") + # Reading original audio file + audio = pydub.AudioSegment.from_file(path) + original_audio_size_ms = audio.duration_seconds * 1000 + + # Fetching mins and secs from audio input + audio_start_time_list = audio_slice_start_time.split(":") + audio_end_time_list = audio_slice_end_time.split(":") + + # Converting audio start and end times in msecs + audio_start_time = convert_audio_time_to_msec(audio_start_time_list) + audio_end_time = convert_audio_time_to_msec(audio_end_time_list) + + # Check if audio start and end times are within original audio size limits + if(audio_start_time > original_audio_size_ms or audio_end_time > original_audio_size_ms): + error("Error! Audio slice input params cannot be greater than original audio size.") + error(" Please try again with correct parameters.") + exit(1) - # Converting audio start and end times in msecs - audio_start_time = convert_audio_time_to_msec(audio_start_time_list) - audio_end_time = convert_audio_time_to_msec(audio_end_time_list) + # Audio slicing process + console.log(f"Slicing {pretty_filename} from {audio_slice_start_time} to {audio_slice_end_time}") + audio = audio[audio_start_time:audio_end_time] + console.log("Slicing complete") + + # Filename for exported file + audio_start_time_name = export_filename(audio_start_time_list) + audio_end_time_name = export_filename(audio_end_time_list) + samplename = f"sampled-{audio_start_time_name}-{audio_end_time_name}-{path.name}" + new_filename = f"{path.parent}/{samplename}" + console.log(f"Exporting file to {samplename}") + audio.export(new_filename, format="mp3") - # Check if audio start and end times are within original audio size limits - if(audio_start_time > original_audio_size_ms or audio_end_time > original_audio_size_ms): - print("Error! Audio slice input params cannot be greater than original audio size. Please try again with correct parameters.") - exit(1) - - # Audio slicing process - audio = audio[audio_start_time:audio_end_time] - - # Filename for exported file - audio_start_time_name = export_filename(audio_start_time_list) - audio_end_time_name = export_filename(audio_end_time_list) - new_filename = f"{path.parent}/sampled-{audio_start_time_name}-{audio_end_time_name}-{path.name}" - audio.export(new_filename, format="mp3") - print("Created new file: ", new_filename) - -def main(): - if len(sys.argv) != 4: - print("Usage: python3 slicer.py