File size: 2,634 Bytes
5a256aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""

    Script for extracting audio (16-bit, mono, 22000 Hz) from video file.

"""

import os
import argparse
import subprocess


def parse_args():
    """

    Create python script parameters.



    Returns

    -------

    ArgumentParser

        Resulted args.

    """
    parser = argparse.ArgumentParser(
        description="Extract audio from video file",
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument(
        "--in-video",
        type=str,
        required=True,
        help="path to input video file or directory")
    parser.add_argument(
        "--out-audio",
        type=str,
        help="path to output audio file")

    args = parser.parse_args()
    return args


def extract_audio(in_video,

                  out_audio):
    """

    Real extract audio from video file.



    Parameters

    ----------

    in_video : str

        Path to input video file.

    out_audio : str

        Path to output audio file.

    """
    if not out_audio:
        file_stem, _ = os.path.splitext(in_video)
        out_audio = file_stem + ".wav"
    # command1 = "ffmpeg -i {in_video} -vn -acodec copy {aac_audio}"
    # command2 = "ffmpeg -i {aac_audio} -vn -acodec pcm_s16le -ac 1 -ar 22000 {out_audio}"
    # command = "ffmpeg -i {in_video} -vn -acodec pcm_s16le -ac 1 -ar 22000 {out_audio}"
    command = "ffmpeg -i {in_video} -vn -acodec pcm_s16le -ac 1 -ar 16000 {out_audio}"
    subprocess.call([command.format(in_video=in_video, out_audio=out_audio)], shell=True)


def main():
    """

    Main body of script.

    """
    args = parse_args()
    in_video = os.path.expanduser(args.in_video)
    if not os.path.exists(in_video):
        raise Exception("Input file/directory doesn't exist: {}".format(in_video))
    if os.path.isfile(in_video):
        extract_audio(
            in_video=in_video,
            out_audio=args.out_audio)
    else:
        video_file_paths = []
        for file_name in os.listdir(in_video):
            if not os.path.isfile(os.path.join(in_video, file_name)):
                continue
            _, file_ext = os.path.splitext(file_name)
            if file_ext.lower() in (".mp4", ".mkv", ".avi"):
                video_file_path = os.path.join(in_video, file_name)
                video_file_paths.append(video_file_path)
        video_file_paths = sorted(video_file_paths)
        for video_file_path in video_file_paths:
            extract_audio(
                in_video=video_file_path,
                out_audio="")


if __name__ == "__main__":
    main()