GNU/Linux >> Linux の 問題 >  >> Ubuntu

Ubuntu – SoxまたはFfmpegを使用したテキストファイルのタイムスタンプに基づいて、オーディオをいくつかの部分に分割しますか?

次のリンクを見ました:開始時間と停止時間を使用してオーディオファイルをトリミングする

しかし、これは私の質問に完全には答えません。私の問題は次のとおりです。abc.mp3などのオーディオファイルがあります またはabc.wav 。開始タイムスタンプと終了タイムスタンプを含むテキストファイルもあります:

0.0 1.0 silence  
1.0 5.0 music  
6.0 8.0 speech    

Pythonとsoxを使用してオーディオを3つの部分に分割したい / ffmpeg 、したがって、3つの別々のオーディオファイルになります。

soxを使用してこれを実現するにはどうすればよいですか またはffmpeg

後で、librosaを使用してそれらの部分に対応するMFCCを計算したいと思います。 。

Python 2.7を持っています 、ffmpeg 、およびsox UbuntuLinux16.04インストールの場合。

承認された回答:

すぐに試してみましたが、テストの方法はほとんどないので、役立つかもしれません。以下はffmpeg-pythonに依存していますが、subprocessで書くのは難しいことではありません。 とにかく。

現時点では、時刻入力ファイルは、開始と終了、そして出力名のペアとして扱われます。欠落している名前はlinecount.wavとして置き換えられます

import ffmpeg
from sys import argv

""" split_wav `audio file` `time listing`

    `audio file` is any file known by local FFmpeg
    `time listing` is a file containing multiple lines of format:
        `start time` `end time` output name 

    times can be either MM:SS or S*
"""

_in_file = argv[1]

def make_time(elem):
    # allow user to enter times on CLI
    t = elem.split(':')
    try:
        # will fail if no ':' in time, otherwise add together for total seconds
        return int(t[0]) * 60 + float(t[1])
    except IndexError:
        return float(t[0])

def collect_from_file():
    """user can save times in a file, with start and end time on a line"""

    time_pairs = []
    with open(argv[2]) as in_times:
        for l, line in enumerate(in_times):
            tp = line.split()
            tp[0] = make_time(tp[0])
            tp[1] = make_time(tp[1]) - tp[0]
            # if no name given, append line count
            if len(tp) < 3:
                tp.append(str(l) + '.wav')
            time_pairs.append(tp)
    return time_pairs

def main():
    for i, tp in enumerate(collect_from_file()):
        # open a file, from `ss`, for duration `t`
        stream = ffmpeg.input(_in_file, ss=tp[0], t=tp[1])
        # output to named file
        stream = ffmpeg.output(stream, tp[2])
        # this was to make trial and error easier
        stream = ffmpeg.overwrite_output(stream)

        # and actually run
        ffmpeg.run(stream)

if __name__ == '__main__':
    main()

Ubuntu
  1. 行に基づいて単一のファイルを複数のファイルに分割する方法

  2. Ubuntu –不要な変更行のある端末から長い1行のテキストをコピーしますか?

  3. UbuntuのデュアルブートからKubuntuをアンインストールしますか?

  1. Ubuntuにdebファイルをインストールする方法(例付き)

  2. 1 つのテキスト ファイルを複数の *.txt ファイルに分割する方法は?

  3. nano を使用してファイルからすべてのテキストを選択するにはどうすればよいですか?

  1. Soxでオーディオファイルのセグメントを分析する方法は?

  2. WindowsのIsoファイルからUbuntuをインストールしますか?

  3. Ubuntuで.isoから.imgファイルを作成する方法は?