如何将音频文件拆分为多个?

Dis*_*ame 64 ffmpeg audio

我找到了一些视频,看起来像这样。

ffmpeg -i * -c:v libx264 -crf 22 -map 0 -segment_time 1 -g 1 -sc_threshold 0 -force_key_frames "expr:gte(t,n_forced*9)" -f segment output%03d.mp4
Run Code Online (Sandbox Code Playgroud)

我尝试将它用于音频文件,但只有第一个音频文件包含实际音频,其他音频文件无声,除此之外它很好,它每秒制作一个新的音频文件。有谁知道要修改什么才能使用音频文件或其他可以执行相同操作的命令?

slm*_*slm 85

当我在 mp3 文件上尝试它时,这对我有用。

$ ffmpeg -i somefile.mp3 -f segment -segment_time 3 -c copy out%03d.mp3
Run Code Online (Sandbox Code Playgroud)

-segment_time每个文件所需的时间在哪里(以秒为单位)。

参考


iso*_*eel 39

要将大音频文件拆分为一组不同长度的轨道,您可以使用以下命令:

# -to is the end time of the sub-file
ffmpeg -i BIG_FILE -acodec copy -ss START_TIME -to END_TIME LITTLE_FILE
Run Code Online (Sandbox Code Playgroud)

例如,我使用包含开始、结束、名称的文本文件将 Inception Original Soundtrack 的单个 .opus 文件分解为子文件:

00:00:00 00:01:11 01_half_remembered_dream
00:01:11 00:03:07 02_we_built_our_own_world
00:03:07 00:05:31 03_dream_is_collapsing
00:05:31 00:09:14 04_radical_notion
00:09:14 00:16:58 05_old_souls
00:16:58 00:19:22 06
00:19:22 00:24:16 07_mombasa
00:24:16 00:26:44 08_one_simple_idea
00:26:44 00:31:49 09_dream_within_a_dream
00:31:49 00:41:19 10_waiting_for_a_train
00:41:19 00:44:44 11_paradox
00:44:44 00:49:20 12_time
Run Code Online (Sandbox Code Playgroud)

我编写了这个简短的awk程序来读取文本文件并ffmpeg从每一行创建命令:

{
    # make ffmpeg command string using sprintf
    cmd = sprintf("ffmpeg -i inception_ost.opus -acodec copy -ss %s -to %s %s.opus", $1, $2, $3)

    # execute ffmpeg command with awk's system function
    system(cmd)
}
Run Code Online (Sandbox Code Playgroud)

这是python名为 的程序的更详细版本split.py,现在从命令行读取原始轨道文件指定子轨道的文本文件:

import subprocess
import sys


def main():
    """split a music track into specified sub-tracks by calling ffmpeg from the shell"""

    # check command line for original file and track list file
    if len(sys.argv) != 3:
        print 'usage: split <original_track> <track_list>'
        exit(1)

    # record command line args
    original_track = sys.argv[1]
    track_list = sys.argv[2]

    # create a template of the ffmpeg call in advance
    cmd_string = 'ffmpeg -i {tr} -acodec copy -ss {st} -to {en} {nm}.opus'

    # read each line of the track list and split into start, end, name
    with open(track_list, 'r') as f:
        for line in f:
            # skip comment and empty lines
            if line.startswith('#') or len(line) <= 1:
                continue

            # create command string for a given track
            start, end, name = line.strip().split()
            command = cmd_string.format(tr=original_track, st=start, en=end, nm=name)

            # use subprocess to execute the command in the shell
            subprocess.call(command, shell=True)

    return None


if __name__ == '__main__':
    main()
Run Code Online (Sandbox Code Playgroud)

您可以ffmpeg通过修改命令模板和/或向 track_list 文件的每一行添加更多字段,轻松构建越来越复杂的调用。

  • @kRazzyR `subprocess.call(command)` 是类似于 `awk` 中的 `system(command)` 的 `python`,两者都允许你向 shell 发送 `ffmpeg` 命令。我编辑了我的帖子,以包含一个灵活的“python”实现,说明了您可以实现的一种方法。 (2认同)
  • 这是一个紧凑的 bash/zsh 版本。根据需要编辑时间,然后删除“echo”一词以实际运行命令。`source="源音频文件.m4a"; 我=0; t1=0:00; 对于 end_time 1:24 5:40 14:48 19:33 35:11 40:00 46:08 51:58 ''; 做 i=$((i+1)); t0=$t1 t1=$end_time; echo ffmpeg -i "$source" -acodec copy -ss $t0 ${t1:+-to} $t1 $(printf "track%02d.%s" $i ${source##*.}); 完成` (2认同)

Str*_*ker 5

以下行将把一个音频文件分割成多个文件,每个文件持续时间为 30 秒。

ffmpeg -i file.wav -f segment -segment_time 30 -c copy parts/output%09d.wav
Run Code Online (Sandbox Code Playgroud)

将 30(秒数)更改为您想要的任何数字。