Spaces:
Runtime error
Runtime error
File size: 5,187 Bytes
b3c65ae 944e83e b7a6480 d72ce9c b7a6480 d72ce9c b7a6480 d72ce9c b7a6480 d72ce9c 214ba1c d72ce9c b7a6480 64a2ea3 b7a6480 64a2ea3 b7a6480 64a2ea3 b7a6480 64a2ea3 944e83e 64a2ea3 944e83e b7a6480 64a2ea3 944e83e 64a2ea3 944e83e 64a2ea3 944e83e 64a2ea3 944e83e 64a2ea3 944e83e 64a2ea3 944e83e 64a2ea3 b7a6480 64a2ea3 0190f40 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | """Audio processing utilities for OutofLipSync"""
import os
import subprocess
from ffmpy import FFmpeg, FFRuntimeError
def get_audio_duration(audio_path: str, max_duration: float = 30.0) -> float:
"""Get audio file duration, raise error if exceeds max_duration
Args:
audio_path: Path to audio file
max_duration: Maximum duration in seconds (default 30)
Returns:
Duration in seconds
Raises:
ValueError: If audio duration exceeds max_duration
"""
cmd = [
"ffprobe",
"-v",
"error",
"-show_entries",
"format=duration",
"-of",
"default=noprint_wrappers=1:nokey=1",
audio_path,
]
result = subprocess.run(cmd, capture_output=True, text=True, check=True)
duration = float(result.stdout.strip())
return duration
# def prepare_target_audio(audio_path: str, output_dir: str) -> tuple:
# """Prepare target audio for lipsync (DEPRECATED - use prepare_audio_for_lipsync instead)
#
# Args:
# audio_path: Path to target audio
# output_dir: Output directory
#
# Returns:
# (audio_16k_path, audio_upsampled_path)
# """
# audio_16k = os.path.join(output_dir, "audio_16k.wav")
# audio_upsampled = os.path.join(output_dir, "audio_upsampled.wav")
#
# ffmpeg1 = FFmpeg(
# inputs={audio_path: None},
# outputs={
# audio_16k: [
# "-ar",
# "16000",
# "-ac",
# "1",
# "-acodec",
# "pcm_s16le",
# "-loglevel",
# "error",
# "-y",
# ]
# },
# )
# try:
# ffmpeg1.run()
# except FFRuntimeError as e:
# raise Exception(f"FFmpeg failed to convert to 16k: {e}")
#
# ffmpeg2 = FFmpeg(
# inputs={audio_16k: None},
# outputs={
# audio_upsampled: [
# "-ar",
# "48000",
# "-ac",
# "1",
# "-acodec",
# "pcm_s16le",
# "-loglevel",
# "error",
# "-y",
# ]
# },
# )
# try:
# ffmpeg2.run()
# except FFRuntimeError as e:
# raise Exception(f"FFmpeg failed to upsample to 48k: {e}")
#
# return audio_16k, audio_upsampled
def prepare_audio_for_lipsync(audio_path: str, output_dir: str) -> str:
"""Chuẩn bị audio 16kHz mono cho lipsync pipeline
Args:
audio_path: Path audio gốc
output_dir: Output directory
Returns:
Path audio 16k WAV
"""
audio_16k = os.path.join(output_dir, "audio_16k.wav")
ffmpeg = FFmpeg(
inputs={audio_path: None},
outputs={
audio_16k: [
"-ar",
"16000",
"-ac",
"1",
"-acodec",
"pcm_s16le",
"-loglevel",
"error",
"-y",
]
},
)
try:
ffmpeg.run()
except FFRuntimeError as e:
raise Exception(f"FFmpeg failed to convert to 16k: {e}")
return audio_16k
def prepare_audio_for_youtube_aac(audio_path: str, output_dir: str) -> str:
"""Chuẩn bị audio theo chuẩn YouTube (AAC)
Args:
audio_path: Path audio gốc
output_dir: Output directory
Returns:
Path audio YouTube (AAC)
"""
from config import (
YOUTUBE_AUDIO_CODEC,
YOUTUBE_AUDIO_BITRATE,
YOUTUBE_AUDIO_SAMPLE_RATE,
)
output_path = os.path.join(output_dir, "audio_youtube.aac")
ffmpeg = FFmpeg(
inputs={audio_path: None},
outputs={
output_path: [
"-ar",
str(YOUTUBE_AUDIO_SAMPLE_RATE),
"-ac",
"2",
"-acodec",
YOUTUBE_AUDIO_CODEC,
"-b:a",
YOUTUBE_AUDIO_BITRATE,
"-loglevel",
"error",
"-y",
]
},
)
try:
ffmpeg.run()
except FFRuntimeError as e:
raise Exception(f"FFmpeg failed to prepare audio for YouTube: {e}")
return output_path
def prepare_audio_for_youtube(audio_path: str, output_dir: str) -> str:
"""
Chuẩn bị audio tối ưu cho YouTube
Args:
audio_path: Path to audio file (WAV)
output_dir: Output directory
Returns:
Path to audio file (WAV 48kHz PCM)
"""
output_path = os.path.join(output_dir, "audio_final.wav")
ffmpeg = FFmpeg(
inputs={audio_path: None},
outputs={
output_path: [
"-ar",
"48000",
"-ac",
"2",
"-acodec",
"pcm_s16le",
"-loglevel",
"error",
"-y",
]
},
)
try:
ffmpeg.run()
except FFRuntimeError as e:
raise Exception(f"FFmpeg failed to prepare audio for YouTube: {e}")
return output_path
|