Welcome to mirror list, hosted at ThFree Co, Russian Federation.

convert_and_trim.py « audiosync « operators « power_sequencer - git.blender.org/blender-addons.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fddf702d1efeea8c33b1a62ad381cc47221c42c9 (plain)
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
#
# Copyright (C) 2016-2020 by Nathan Lovato, Daniel Oakey, Razvan Radulescu, and contributors
#
# This file is part of Power Sequencer.
#
# Power Sequencer is free software: you can redistribute it and/or modify it under the terms of the
# GNU General Public License as published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# Power Sequencer is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
# without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with Power Sequencer. If
# not, see <https://www.gnu.org/licenses/>.
#
import subprocess
import tempfile


def convert_and_trim(audio_filepath, freq, dur):
    """
    Uses ffmpeg to convert an audio file to a temporary wav file for use
    in finding offset.

    Args
        :audio: path to the audiofile to convert (string)
        :freq:  Samples / second in the output wav (int)
        :dur:   Max duration of the output wav in seconds (float)

    Returns
        :outpath: path to the output wav file
    """

    tmp = tempfile.NamedTemporaryFile(mode="r+b", prefix="offset_", suffix=".wav")
    outpath = tmp.name
    tmp.close()

    channel_count = "1"

    subprocess.call(
        [
            "ffmpeg",
            "-loglevel",
            "panic",
            "-i",
            audio_filepath,
            "-ac",
            channel_count,
            "-ar",
            str(freq),
            "-t",
            str(dur),
            "-acodec",
            "pcm_s16le",
            outpath,
        ]
    )

    return outpath