voxscribe.voxscribe

  1import base64
  2import os
  3from datetime import datetime
  4from pathlib import Path
  5
  6import requests
  7import speech_recognition
  8from pydub import AudioSegment
  9from whosyouragent import get_agent
 10
 11root = Path(__file__).parent
 12
 13""" Extract text from an mp3 or wav file. """
 14
 15
 16def download_audio_file(url: str, file_ext: str) -> Path:
 17    """Downloads an audio file to
 18    a folder named audio in
 19    the same folder as this file.
 20
 21    :param file_ext: Can be either '.mp3' or '.wav'.
 22
 23    Returns a Path object for the saved file."""
 24    dest = root / "audio"
 25    dest.mkdir(parents=True, exist_ok=True)
 26    filepath = (dest / str(datetime.now().timestamp())).with_suffix(file_ext)
 27    source = requests.get(url, headers={"User-Agent": get_agent()})
 28    print(f"{source.status_code=}")
 29    with filepath.open("wb") as file:
 30        file.write(source.content)
 31    return filepath
 32
 33
 34def base64_to_audiofile(src: str, file_ext: str) -> Path:
 35    """Convert and save base64 string to an audio file.
 36
 37    :param src: The base64 encoded string.
 38
 39    :param file_ext: Can be either '.mp3' or '.wav'.
 40
 41    Returns a Path object for the saved file."""
 42    dest = root / "audio"
 43    dest.mkdir(parents=True, exist_ok=True)
 44    filepath = (dest / str(datetime.now().timestamp())).with_suffix(file_ext)
 45    filepath.write_bytes(base64.b64decode(src))
 46    return filepath
 47
 48
 49def convert_MP3_to_WAV(MP3path: Path | str) -> Path:
 50    """Converts an mp3 file to a wav file
 51    of the same name and returns a Path object
 52    for the wav file."""
 53    MP3path = Path(MP3path)
 54    audio = AudioSegment.from_mp3(MP3path)
 55    WAVpath = MP3path.with_suffix(".wav")
 56    audio.export(WAVpath, format="wav")
 57    return WAVpath
 58
 59
 60def get_text_from_url(url: str, file_ext: str) -> str:
 61    """Returns text from an mp3 file
 62    located at the given url.
 63
 64    :param file_ext: Can be either '.mp3' or '.wav'"""
 65    audiopath = download_audio_file(url, file_ext)
 66    if file_ext == ".mp3":
 67        return get_text_from_WAV(convert_MP3_to_WAV(audiopath))
 68    elif file_ext == ".wav":
 69        return get_text_from_WAV(audiopath)
 70    else:
 71        raise Exception('file_ext param must be ".mp3" or ".wav"')
 72
 73
 74def get_text_from_WAV(WAVpath: Path | str) -> str:
 75    """Returns text from a wav file
 76    located at the give file path."""
 77    WAVpath = Path(WAVpath)
 78    recognizer = speech_recognition.Recognizer()
 79    with speech_recognition.AudioFile(str(WAVpath)) as source:
 80        audio = recognizer.record(source)
 81        text = recognizer.recognize_google(audio)
 82    return text
 83
 84
 85def get_text_from_MP3(MP3path: Path | str) -> str:
 86    """Returns text from an mp3 file
 87    located at the give file path."""
 88    return get_text_from_WAV(convert_MP3_to_WAV(MP3path))
 89
 90
 91def get_text_from_base64(src: str, file_ext: str) -> str:
 92    """Returns text from a base64 encoded audio string.
 93
 94    :param src: The base64 encoded auio.
 95
 96    :param file_ext: Can me '.mp3' or '.wav'."""
 97    filepath = base64_to_audiofile(src, file_ext)
 98    match file_ext:
 99        case ".wav":
100            return get_text_from_WAV(filepath)
101        case ".mp3":
102            return get_text_from_MP3(filepath)
103
104
105def clean_up(max_age: int):
106    """Removes any files from the audio directory
107    older than max_age minutes."""
108    audiopath = root / "audio"
109    if audiopath.exists():
110        for file in audiopath.glob("*.*"):
111            if (datetime.now().timestamp() - os.stat(file).st_ctime) > (60 * max_age):
112                file.unlink()
root = WindowsPath('E:/1vsCode/python/voxscribe/src/voxscribe')

Extract text from an mp3 or wav file.

def download_audio_file(url: str, file_ext: str) -> pathlib.Path:
17def download_audio_file(url: str, file_ext: str) -> Path:
18    """Downloads an audio file to
19    a folder named audio in
20    the same folder as this file.
21
22    :param file_ext: Can be either '.mp3' or '.wav'.
23
24    Returns a Path object for the saved file."""
25    dest = root / "audio"
26    dest.mkdir(parents=True, exist_ok=True)
27    filepath = (dest / str(datetime.now().timestamp())).with_suffix(file_ext)
28    source = requests.get(url, headers={"User-Agent": get_agent()})
29    print(f"{source.status_code=}")
30    with filepath.open("wb") as file:
31        file.write(source.content)
32    return filepath

Downloads an audio file to a folder named audio in the same folder as this file.

Parameters
  • file_ext: Can be either '.mp3' or '.wav'.

Returns a Path object for the saved file.

def base64_to_audiofile(src: str, file_ext: str) -> pathlib.Path:
35def base64_to_audiofile(src: str, file_ext: str) -> Path:
36    """Convert and save base64 string to an audio file.
37
38    :param src: The base64 encoded string.
39
40    :param file_ext: Can be either '.mp3' or '.wav'.
41
42    Returns a Path object for the saved file."""
43    dest = root / "audio"
44    dest.mkdir(parents=True, exist_ok=True)
45    filepath = (dest / str(datetime.now().timestamp())).with_suffix(file_ext)
46    filepath.write_bytes(base64.b64decode(src))
47    return filepath

Convert and save base64 string to an audio file.

Parameters
  • src: The base64 encoded string.

  • file_ext: Can be either '.mp3' or '.wav'.

Returns a Path object for the saved file.

def convert_MP3_to_WAV(MP3path: pathlib.Path | str) -> pathlib.Path:
50def convert_MP3_to_WAV(MP3path: Path | str) -> Path:
51    """Converts an mp3 file to a wav file
52    of the same name and returns a Path object
53    for the wav file."""
54    MP3path = Path(MP3path)
55    audio = AudioSegment.from_mp3(MP3path)
56    WAVpath = MP3path.with_suffix(".wav")
57    audio.export(WAVpath, format="wav")
58    return WAVpath

Converts an mp3 file to a wav file of the same name and returns a Path object for the wav file.

def get_text_from_url(url: str, file_ext: str) -> str:
61def get_text_from_url(url: str, file_ext: str) -> str:
62    """Returns text from an mp3 file
63    located at the given url.
64
65    :param file_ext: Can be either '.mp3' or '.wav'"""
66    audiopath = download_audio_file(url, file_ext)
67    if file_ext == ".mp3":
68        return get_text_from_WAV(convert_MP3_to_WAV(audiopath))
69    elif file_ext == ".wav":
70        return get_text_from_WAV(audiopath)
71    else:
72        raise Exception('file_ext param must be ".mp3" or ".wav"')

Returns text from an mp3 file located at the given url.

Parameters
  • file_ext: Can be either '.mp3' or '.wav'
def get_text_from_WAV(WAVpath: pathlib.Path | str) -> str:
75def get_text_from_WAV(WAVpath: Path | str) -> str:
76    """Returns text from a wav file
77    located at the give file path."""
78    WAVpath = Path(WAVpath)
79    recognizer = speech_recognition.Recognizer()
80    with speech_recognition.AudioFile(str(WAVpath)) as source:
81        audio = recognizer.record(source)
82        text = recognizer.recognize_google(audio)
83    return text

Returns text from a wav file located at the give file path.

def get_text_from_MP3(MP3path: pathlib.Path | str) -> str:
86def get_text_from_MP3(MP3path: Path | str) -> str:
87    """Returns text from an mp3 file
88    located at the give file path."""
89    return get_text_from_WAV(convert_MP3_to_WAV(MP3path))

Returns text from an mp3 file located at the give file path.

def get_text_from_base64(src: str, file_ext: str) -> str:
 92def get_text_from_base64(src: str, file_ext: str) -> str:
 93    """Returns text from a base64 encoded audio string.
 94
 95    :param src: The base64 encoded auio.
 96
 97    :param file_ext: Can me '.mp3' or '.wav'."""
 98    filepath = base64_to_audiofile(src, file_ext)
 99    match file_ext:
100        case ".wav":
101            return get_text_from_WAV(filepath)
102        case ".mp3":
103            return get_text_from_MP3(filepath)

Returns text from a base64 encoded audio string.

Parameters
  • src: The base64 encoded auio.

  • file_ext: Can me '.mp3' or '.wav'.

def clean_up(max_age: int):
106def clean_up(max_age: int):
107    """Removes any files from the audio directory
108    older than max_age minutes."""
109    audiopath = root / "audio"
110    if audiopath.exists():
111        for file in audiopath.glob("*.*"):
112            if (datetime.now().timestamp() - os.stat(file).st_ctime) > (60 * max_age):
113                file.unlink()

Removes any files from the audio directory older than max_age minutes.