Skip to content

Commit

Permalink
Merge pull request #90 from historysciencelab/slicer-print-dependency…
Browse files Browse the repository at this point in the history
…-improvements

Dependency and notification Improvements to slicer
  • Loading branch information
audreyfeldroy authored Oct 29, 2023
2 parents f761c27 + a599178 commit 87579e1
Showing 1 changed file with 89 additions and 68 deletions.
157 changes: 89 additions & 68 deletions src/interviewkit/slicer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -28,93 +28,114 @@
"""
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 """
"""Converting mins and secs to msecs for pydub computation"""

if(audio_time_split_list):
if(len(audio_time_split_list) == 1):
if audio_time_split_list:
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
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):
""" Filename for exported file """
"""Filename for exported file"""

if audio_time_list and len(audio_time_list) == 2:
return f"{audio_time_list[0]}m{audio_time_list[1]}s"
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.")
exit(1)


def audio_slicing(path, audio_slice_start_time, audio_slice_end_time):
""" 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(":")

# 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):
print("Error! Audio slice input params cannot be greater than original audio size. Please try again with correct parameters.")
error(
"Error! Audio slice input params invalid. Please try again with correct parameters."
)
error("Error inside export_filename(audio_time_list) funtion.")
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 <filepath> <audio start time in minutes> <audio end time in minutes>")
return

path = Path(sys.argv[1])
audio_slice_start_time = (sys.argv[2])
audio_slice_end_time = (sys.argv[3])

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."""

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)

if __name__ == '__main__':
main()
# 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")

console.print("Export complete!", style="bold")
console.print(path, style="bold green")

0 comments on commit 87579e1

Please sign in to comment.