aliSaac510 commited on
Commit
0157334
·
1 Parent(s): 65d3282

Optimize audio extraction using FFmpeg directly for faster processing

Browse files
Files changed (1) hide show
  1. video_processor.py +50 -12
video_processor.py CHANGED
@@ -325,24 +325,62 @@ def extract_audio_from_video(video_path: str, output_format: str = "mp3"):
325
  Path to the extracted audio file
326
  """
327
  try:
328
- from moviepy import VideoFileClip
329
-
330
  # Generate output filename
331
  base_name = os.path.splitext(os.path.basename(video_path))[0]
332
  audio_filename = f"{base_name}_audio.{output_format}"
333
  output_path = os.path.join(os.path.dirname(video_path), audio_filename)
334
 
335
- # Extract audio
336
- with VideoFileClip(video_path) as video:
337
- if video.audio is None:
338
- raise ValueError("Video has no audio track")
339
 
340
- video.audio.write_audiofile(
341
- output_path,
342
- logger=None
343
- )
344
-
345
- return output_path
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
346
 
347
  except Exception as e:
348
  print(f"Error extracting audio: {str(e)}")
 
325
  Path to the extracted audio file
326
  """
327
  try:
 
 
328
  # Generate output filename
329
  base_name = os.path.splitext(os.path.basename(video_path))[0]
330
  audio_filename = f"{base_name}_audio.{output_format}"
331
  output_path = os.path.join(os.path.dirname(video_path), audio_filename)
332
 
333
+ # Try FFmpeg first (fastest method)
334
+ try:
335
+ import subprocess
 
336
 
337
+ # Determine codec based on output format
338
+ if output_format.lower() == 'mp3':
339
+ codec = 'libmp3lame'
340
+ elif output_format.lower() == 'wav':
341
+ codec = 'pcm_s16le'
342
+ elif output_format.lower() in ['m4a', 'aac']:
343
+ codec = 'aac'
344
+ else:
345
+ codec = 'copy' # Try to copy without re-encoding
346
+
347
+ # FFmpeg command to extract audio stream only (much faster)
348
+ cmd = [
349
+ 'ffmpeg', '-i', video_path,
350
+ '-vn', # no video processing
351
+ '-acodec', codec,
352
+ '-y', # overwrite output
353
+ '-loglevel', 'error', # suppress output
354
+ output_path
355
+ ]
356
+
357
+ result = subprocess.run(cmd, capture_output=True, text=True)
358
+
359
+ if result.returncode == 0 and os.path.exists(output_path):
360
+ print(f"✓ Audio extracted using FFmpeg (fast method)")
361
+ return output_path
362
+ else:
363
+ print(f"FFmpeg failed: {result.stderr}")
364
+ raise Exception("FFmpeg extraction failed")
365
+
366
+ except Exception as ffmpeg_error:
367
+ print(f"FFmpeg not available or failed: {ffmpeg_error}")
368
+ print("Falling back to MoviePy method...")
369
+
370
+ # Fallback to MoviePy method
371
+ from moviepy import VideoFileClip
372
+
373
+ with VideoFileClip(video_path) as video:
374
+ if video.audio is None:
375
+ raise ValueError("Video has no audio track")
376
+
377
+ video.audio.write_audiofile(
378
+ output_path,
379
+ logger=None
380
+ )
381
+
382
+ print(f"✓ Audio extracted using MoviePy (fallback method)")
383
+ return output_path
384
 
385
  except Exception as e:
386
  print(f"Error extracting audio: {str(e)}")