Skip to content

Audio


Audio processing module for video files.

This module provides functionality for converting video files to audio format, specifically focusing on MP4 to WAV conversion for further processing.

Functions:

Name Description
convert_mp4_to_wav

Converts MP4 video files to WAV audio format

convert_mp4_to_wav(video_path, video_id)

Convert MP4 video file to WAV audio format.

Parameters:

Name Type Description Default
video_path str

Directory path containing the video file

required
video_id str

Identifier of the video file

required

Returns:

Type Description
Path

Path object pointing to the output WAV file

Raises:

Type Description
Exception

If FFMPEG subprocess fails during conversion

Source code in apps/annotator/code/media/audio.py
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
def convert_mp4_to_wav(video_path: str, video_id: str) -> Path:
    """
    Convert MP4 video file to WAV audio format.

    Parameters
    ----------
    video_path : str
        Directory path containing the video file
    video_id : str
        Identifier of the video file

    Returns
    -------
    Path
        Path object pointing to the output WAV file

    Raises
    ------
    Exception
        If FFMPEG subprocess fails during conversion
    """
    output_file_path = Path(video_path).joinpath(video_id+'.wav')
    if os.path.isfile(output_file_path): 
        return output_file_path

    input_file_path = Path(video_path).joinpath(video_id+'.mp4').__str__()
    try:
        # Run ffmpeg command to convert MP4 to WAV
        subprocess.run(['ffmpeg', '-i', input_file_path, output_file_path.__str__()], 
                       check=True, 
                       stdout=subprocess.PIPE, 
                       stderr=subprocess.PIPE)
    except subprocess.CalledProcessError as e:
        print(e)
        raise Exception("ERROR SUBPROCESS FFMPEG")
    return output_file_path