如何将大型单轨mp3剪切为多轨

时间:2019-06-16 15:28:14

标签: python audio

我无法理解如何正确使用PyDub。我想将较大的mp3剪切为较小的文件,尽管我无法实现使用起点和终点正确剪切每个曲目的方法。

我使用了一种方法,将定义的端点传递到数组中,然后使用该方法来剪切轨道。但是,这就引入了问题,即歌曲每次从0开始播放,即0-2:34、0-5:34、0-9:32等。

我尝试运行时也会遇到内存错误

PS H:\> & C:/Users/Luke/AppData/Local/Programs/Python/Python37-32/python.exe h:/song_slice.py
Exception in thread Thread-3:
Traceback (most recent call last):
  File "C:\Users\Luke\AppData\Local\Programs\Python\Python37-32\lib\threading.py", line 917, in _bootstrap_inner
    self.run()
  File "C:\Users\Luke\AppData\Local\Programs\Python\Python37-32\lib\threading.py", line 865, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\Luke\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 1238, in _readerthread 
    buffer.append(fh.read())
MemoryError

import pandas as pd
from pydub import AudioSegment

global tracks

# track list no. array
tracklisting = []
# track list song endpoint array
tracklisting_range = []
# the mp3 file
sound = AudioSegment.from_mp3("somemix.mp3")

def track():
    tracks = int(input("How many tracks?"))
    while True:
        if (tracks > 0):
            return tracks
        else:
            tracks = int(input("Invalid number, please try again:"))


if __name__ == "__main__":
    no_track = track()

    # takes amount of tracks, ask endpoint for each, stores them in a list

    for i in range(no_track):
        temp_val = int(input("Select endpoint for for Track {0} ".format(i)))
        tracklisting.append(temp_val)

    tracklisting_range = pd.Series(tracklisting)
    tracklisting_range = tracklisting_range.astype(int)

    # know we have an array full of end points in milliseconds
    print((tracklisting_range * 100000).to_list())

# pass list so it can cut music
# TODO figure out how to cut song from last endpoint

for i in range(tracklisting_range):
    x = x + 1
    track = sound[i:]
    track.export("Track %x", format="mp3")

我想要的结果是已经能够知道最后一个切割点在哪里,然后从那里开始。但是,浏览文档时,我看不到如何进行设置。从我的角度来看,这首歌是从0开始的,您只能设置要剪切的位置,而不是剪切的位置。

0 个答案:

没有答案