mirror of
https://github.com/tcsenpai/quick_audio_cloner.git
synced 2025-06-06 03:05:26 +00:00
added mp3 conversion support
This commit is contained in:
parent
66ca90a9de
commit
92d765f550
50
src/libs/to_mp3.py
Normal file
50
src/libs/to_mp3.py
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
from pathlib import Path
|
||||||
|
from pydub import AudioSegment
|
||||||
|
|
||||||
|
|
||||||
|
def convert_to_mp3(wav_path: str, bitrate: str = "192k") -> str:
|
||||||
|
"""
|
||||||
|
Convert a WAV file to MP3 format in the same directory.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
wav_path (str): Path to the WAV file
|
||||||
|
bitrate (str): MP3 bitrate (default: "192k")
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
str: Path to the created MP3 file
|
||||||
|
"""
|
||||||
|
# Convert string path to Path object
|
||||||
|
wav_path = Path(wav_path)
|
||||||
|
|
||||||
|
# Check if input file exists and is WAV
|
||||||
|
if not wav_path.exists():
|
||||||
|
raise FileNotFoundError(f"File not found: {wav_path}")
|
||||||
|
if wav_path.suffix.lower() != ".wav":
|
||||||
|
raise ValueError(f"Input file must be a WAV file, got: {wav_path.suffix}")
|
||||||
|
|
||||||
|
# Create output path with same name but .mp3 extension
|
||||||
|
mp3_path = wav_path.with_suffix(".mp3")
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Load WAV and export as MP3
|
||||||
|
audio = AudioSegment.from_wav(str(wav_path))
|
||||||
|
audio.export(str(mp3_path), format="mp3", bitrate=bitrate)
|
||||||
|
return str(mp3_path)
|
||||||
|
except Exception as e:
|
||||||
|
raise Exception(f"Error converting WAV to MP3: {str(e)}")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
import argparse
|
||||||
|
|
||||||
|
parser = argparse.ArgumentParser(description="Convert WAV to MP3")
|
||||||
|
parser.add_argument("wav_path", help="Path to the WAV file to convert")
|
||||||
|
parser.add_argument("--bitrate", default="192k", help="MP3 bitrate (default: 192k)")
|
||||||
|
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
try:
|
||||||
|
output_path = convert_to_mp3(args.wav_path, args.bitrate)
|
||||||
|
print(f"Successfully converted to: {output_path}")
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error: {str(e)}")
|
@ -3,6 +3,7 @@ import torch
|
|||||||
from TTS.api import TTS
|
from TTS.api import TTS
|
||||||
import os
|
import os
|
||||||
import dotenv
|
import dotenv
|
||||||
|
from libs.to_mp3 import convert_to_mp3
|
||||||
from libs.youtube_wav import download_from_cli
|
from libs.youtube_wav import download_from_cli
|
||||||
import os
|
import os
|
||||||
|
|
||||||
@ -133,7 +134,12 @@ def start_job():
|
|||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Error playing audio: {str(e)}")
|
print(f"Error playing audio: {str(e)}")
|
||||||
|
# Ask to convert to mp3
|
||||||
|
convert_response = (
|
||||||
|
input("\nWould you like to convert the output file to mp3? [y/N] ").strip().lower()
|
||||||
|
)
|
||||||
|
if convert_response in ["y", "yes"]:
|
||||||
|
convert_to_mp3(outfile)
|
||||||
|
|
||||||
def set_target_voice():
|
def set_target_voice():
|
||||||
"""Set the target voice for TTS."""
|
"""Set the target voice for TTS."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user