File size: 1,431 Bytes
3ae1bf1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from moviepy import VideoFileClip
import os

def extract_audio(video_path, output_audio_path=None):
    """

    Extract audio from a video file.

    

    Parameters:

    - video_path (str): Path to the video file

    - output_audio_path (str, optional): Path where the audio will be saved. If None,

      it will be saved in the 'audio' directory with the same name as the video file.

      

    Returns:

    - str: Path to the extracted audio file

    """
    # Create audio directory if not specified in output path
    if output_audio_path is None:
        audio_dir = "audio"
        os.makedirs(audio_dir, exist_ok=True)
        
        # Extract the base name of the video file (without extension)
        base_name = os.path.splitext(os.path.basename(video_path))[0]
        
        # Define the output audio file path within the audio directory
        output_audio_path = os.path.join(audio_dir, f"{base_name}.mp3")
    else:
        # Ensure directory exists for the specified output path
        os.makedirs(os.path.dirname(os.path.abspath(output_audio_path)), exist_ok=True)
    
    # Load the video file
    video = VideoFileClip(video_path)

    # Extract audio
    audio = video.audio

    # Save the audio file
    audio.write_audiofile(output_audio_path)

    # Close the video and audio clips
    audio.close()
    video.close()
    
    return output_audio_path