Music_Splitter / video_processor.py
Omar-youssef's picture
Moved Music_Splitter contents into the main repository
c9c6048
raw
history blame
1.55 kB
from pathlib import Path
import subprocess
class VideoProcessor:
def __init__(self, input_video, output_dir):
self.input_video = Path(input_video)
self.output_dir = Path(output_dir)
self.output_dir.mkdir(parents=True, exist_ok=True)
self.temp_audio = self.output_dir / "extracted_audio.wav"
self.final_video = self.output_dir / "final_video.mp4"
def extract_audio(self):
"""Extract audio from video file"""
try:
subprocess.run([
"ffmpeg", "-i", str(self.input_video),
"-vn", "-acodec", "pcm_s16le", "-ar", "44100",
str(self.temp_audio)
], check=True)
print(f"Audio extracted successfully to {self.temp_audio}")
return str(self.temp_audio)
except subprocess.CalledProcessError as e:
print(f"Error extracting audio: {e}")
return None
def combine_video_audio(self, vocals_path):
"""Combine original video with vocals only"""
try:
subprocess.run([
"ffmpeg",
"-i", str(self.input_video),
"-i", vocals_path,
"-c:v", "copy",
"-c:a", "aac",
"-map", "0:v:0",
"-map", "1:a:0",
str(self.final_video)
], check=True)
return str(self.final_video)
except subprocess.CalledProcessError as e:
print(f"Error combining video and audio: {e}")
return None