Updated to use new ffmpeg
This commit is contained in:
1 parent
aa794e458b
commit
231bc21fa7
1 file changed
+17
-23
@@ -10,7 +10,7 @@ import sys
|
|||||||
import os
|
import os
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import asyncio
|
import asyncio
|
||||||
from ffmpeg.asyncio import FFmpeg
|
import ffmpeg
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
@@ -19,34 +19,28 @@ this_dir = Path(__file__).resolve().parent.parent.parent.parent.parent
|
|||||||
|
|
||||||
def load_audio(file, sampling_rate):
|
def load_audio(file, sampling_rate):
|
||||||
try:
|
try:
|
||||||
# Get the parent directory of the current file
|
file = str(file).strip(" ").strip('"').strip("\n").strip('"').strip(" ")
|
||||||
this_dir = Path(__file__).resolve().parent.parent.parent.parent.parent
|
|
||||||
file = file.strip(" ").strip('"').strip("\n").strip('"').strip(" ")
|
|
||||||
|
|
||||||
# Set the ffmpeg_path variable based on the operating system
|
try:
|
||||||
if sys.platform == "win32":
|
# Use ffmpeg-python
|
||||||
ffmpeg_path = os.path.join(this_dir, "system", "win_ffmpeg", "ffmpeg.exe")
|
stream = (
|
||||||
else:
|
ffmpeg
|
||||||
ffmpeg_path = "ffmpeg" # Default path for Linux and macOS
|
.input(file)
|
||||||
|
.output('pipe:', format='f32le', acodec='pcm_f32le', ac=1, ar=str(sampling_rate))
|
||||||
# Initialize the process variable
|
.run(capture_stdout=True, capture_stderr=True)
|
||||||
process = subprocess.Popen(
|
|
||||||
[ffmpeg_path, "-y", "-i", file, "-f", "f32le", "-acodec", "pcm_f32le", "-ac", "1", "-ar", str(sampling_rate), "pipe:1"],
|
|
||||||
stdout=subprocess.PIPE,
|
|
||||||
stderr=subprocess.PIPE
|
|
||||||
)
|
)
|
||||||
|
out = stream[0] # Get stdout data
|
||||||
|
|
||||||
out, err = process.communicate()
|
except ffmpeg.Error as e:
|
||||||
if process.returncode != 0:
|
print(f"FFmpeg error: {e.stderr.decode('utf-8')}")
|
||||||
print(f"FFmpeg error: {err.decode('utf-8')}") # Debug statement
|
raise RuntimeError(f"FFmpeg error: {e.stderr.decode('utf-8')}") from e
|
||||||
raise RuntimeError(f"FFmpeg error: {err.decode('utf-8')}")
|
|
||||||
#print("Audio loaded successfully") # Debug statement
|
|
||||||
except Exception as error:
|
|
||||||
print(f"Error loading audio: {error}") # Debug statement
|
|
||||||
raise RuntimeError(f"Failed to load audio: {error}")
|
|
||||||
|
|
||||||
return np.frombuffer(out, np.float32).flatten()
|
return np.frombuffer(out, np.float32).flatten()
|
||||||
|
|
||||||
|
except Exception as error:
|
||||||
|
print(f"Error loading audio: {error}")
|
||||||
|
raise RuntimeError(f"Failed to load audio: {error}") from error
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def format_title(title):
|
def format_title(title):
|
||||||
|
|||||||
Reference in new issue
Block a user