Để chia một tệp âm thanh lớn thành một tập các bản nhạc có độ dài khác nhau, bạn có thể sử dụng lệnh sau:
# -to is the end time of the sub-file
ffmpeg -i BIG_FILE -acodec copy -ss START_TIME -to END_TIME LITTLE_FILE
Ví dụ: tôi đã chia một tệp .opus của Soundtrack gốc Inception thành các tệp phụ bằng cách sử dụng tệp văn bản này có chứa start, end, name:
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
Tôi đã viết awkchương trình ngắn này để đọc tệp văn bản và tạo ffmpegcác lệnh từ mỗi dòng:
{
# 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)
}
Đây là một pythonphiên bản chi tiết hơn của chương trình được gọi split.py, trong đó bây giờ cả tệp theo dõi gốc và tệp văn bản chỉ định các bản nhạc phụ được đọc từ dòng lệnh:
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()
Bạn có thể dễ dàng xây dựng ffmpegcác cuộc gọi ngày càng phức tạp hơn bằng cách sửa đổi mẫu lệnh và / hoặc thêm nhiều trường vào mỗi dòng của tệp track_list.