GNU/Linux >> Linux Esercitazione >  >> Ubuntu

Ubuntu:dividere l'audio in più parti in base ai timestamp da un file di testo con Sox o Ffmpeg?

Ho guardato il seguente link:Taglia il file audio usando i tempi di inizio e fine

Ma questo non risponde completamente alla mia domanda. Il mio problema è:ho un file audio come abc.mp3 o abc.wav . Ho anche un file di testo contenente timestamp di inizio e fine:

0.0 1.0 silence  
1.0 5.0 music  
6.0 8.0 speech    

Voglio dividere l'audio in tre parti usando Python e sox /ffmpeg , risultando così in tre file audio separati.

Come posso ottenere questo risultato utilizzando sox o ffmpeg ?

Successivamente voglio calcolare l'MFCC corrispondente a quelle porzioni usando librosa .

Ho Python 2.7 , ffmpeg e sox su un'installazione di Ubuntu Linux 16.04.

Risposta accettata:

L'ho appena provato velocemente, molto poco in termini di test, quindi forse sarà d'aiuto. Di seguito si basa su ffmpeg-python, ma non sarebbe una sfida scrivere con subprocess comunque.

Al momento il file di input dell'ora viene trattato solo come coppie di tempi, inizio e fine, e quindi un nome di output. I nomi mancanti vengono sostituiti come 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. Come installare il file deb in Ubuntu (con esempi)

  2. Ubuntu:copiare un lungo testo a riga singola da un terminale con una riga di cambio indesiderata?

  3. Come analizzare un segmento di un file audio con Sox?

  4. Come installare Ubuntu dal file ISO?

  5. Disinstallare Kubuntu da un dual-boot con Ubuntu?

Come gestire i file con Scilab su Ubuntu 15.04

Come visualizzare i dati da un file di testo in Linux

Converti testo in voce con eSpeak su Ubuntu

Come dividere un file Mp3?

Come rendere compatibile il file di testo creato in Ubuntu con il blocco note di Windows?

Come trasferire file da Ubuntu a Ipad Air 4 con chiavetta USB