要将大音频文件分成长度不等的一组音轨,可以使用以下命令:
# -to is the end time of the sub-file
ffmpeg -i BIG_FILE -acodec copy -ss START_TIME -to END_TIME LITTLE_FILE
例如,我使用该文本文件将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
我编写了这个简短的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)
}
这是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()
您可以ffmpeg
通过修改命令模板和/或在track_list文件的每一行中添加更多字段来轻松构建越来越复杂的调用。