voxscribe.voxscribe API documentation

# voxscribe.voxscribe

View Source

      1import base64
      2import os
      3from datetime import datetime
      4from pathlib import Path
      5
      6import requests
      7import speech_recognition
      8from pydub import AudioSegment
      9
     10from whosyouragent import get_agent
     11
     12root = Path(__file__).parent
     13
     14""" Extract text from an mp3 or wav file. """
     15
     16
     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
     33
     34
     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
     48
     49
     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
     59
     60
     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"')
     73
     74
     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
     84
     85
     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))
     90
     91
     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)
    104
    105
    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()

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: View Source

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

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

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

    62def get_text_from_url(url: str, file_ext: str) -> str:
    63    """Returns text from an mp3 file
    64    located at the given url.
    65
    66    :param file_ext: Can be either '.mp3' or '.wav'"""
    67    audiopath = download_audio_file(url, file_ext)
    68    if file_ext == ".mp3":
    69        return get_text_from_WAV(convert_MP3_to_WAV(audiopath))
    70    elif file_ext == ".wav":
    71        return get_text_from_WAV(audiopath)
    72    else:
    73        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: View Source

    76def get_text_from_WAV(WAVpath: Path | str) -> str:
    77    """Returns text from a wav file
    78    located at the give file path."""
    79    WAVpath = Path(WAVpath)
    80    recognizer = speech_recognition.Recognizer()
    81    with speech_recognition.AudioFile(str(WAVpath)) as source:
    82        audio = recognizer.record(source)
    83        text = recognizer.recognize_google(audio)
    84    return text

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

def get_text_from_MP3(MP3path: pathlib.Path | str) -> str: View Source

    87def get_text_from_MP3(MP3path: Path | str) -> str:
    88    """Returns text from an mp3 file
    89    located at the give file path."""
    90    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: View Source

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

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

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