在Django中使用FFMPAG将“从wav音频文件提取的区域转换为Flac音频文件” python

时间:2019-04-09 13:41:16

标签: python django ffmpeg

从wav音频文件中提取的区域具有“ ss的无效持续时间规范” 例如,在我的情况下,持续时间就是这种格式

[(0.006000000000000005, 1.03), (2.0540000000000003, 4.870000000000003)]

这是用于将区域转换为Flac格式。

     class FLACConverter(object):
         # pylint: disable=too-few-public-methods
    """
         Class for converting a region of an input audio or video file into a FLAC audio file
    """

     def __init__(self, source_path, include_before=0.25, include_after=0.25):
        self.source_path = source_path
        self.include_before = include_before
        self.include_after = include_after


     def __call__(self, region):
        try:
            print("regions to convert in flac:{}".format(region))
            start = region
            end = region
          #  start = max(0, start - self.include_before)
            start = list(map(lambda x: tuple(max(0, y - self.include_before) for y in x), start))
            end = list(map(lambda x: tuple(y + self.include_after for y in x), end))
            temp = tempfile.NamedTemporaryFile(suffix='.flac', delete=False)
            command = ["ffmpeg", "-ss", str(start), "-t", str([tuple(x-y for x, y in zip(x1, x2)) for (x1, x2) in zip(end, start)]),
                       "-y", "-i", self.source_path,
                       "-loglevel", "error", temp.name]
            use_shell = True if os.name == "nt" else False
            subprocess.check_output(command)
            print(temp.name)
            #subprocess.check_output(command, stdin=open(os.devnull), shell=use_shell)
            read_data = temp.read()
            temp.close()
            os.unlink(temp.name)
            print("read_data :{}".format(read_data))
            print("temp :{}".format(temp.name))

            return read_data

        except KeyboardInterrupt:
            return None

我希望输出/var/folders/p1/6ttydjfx2sq9zl4cnmjxgjh40000gp/T/tmpwz5n4fnv.flac,但返回错误

  

位于/

的CalledProcessError
Command '['ffmpeg', '-ss', '[(0.006000000000000005, 1.03), (2.0540000000000003, 4.870000000000003)]', '-t', '[(0.5, 0.5), (0.5, 0.5)]', '-y', '-i', '/var/folders/p1/6ttydjfx2sq9zl4cnmjxgjh40000gp/T/tmpuyi5spat.wav', '-loglevel', 'error', '/var/folders/p1/6ttydjfx2sq9zl4cnmjxgjh40000gp/T/tmp0vdewoyd.flac']' returned non-zero exit status 1.```

0 个答案:

没有答案
相关问题