def clip_audio(specs, raw_audio, output):
# Load the spec data. In clipping audio, we hold the specs fixed.
spec_filenames = next(os.walk(specs))[2]
if len(spec_filenames) == 0:
print("No specs found.")
return
for spec_filename in spec_filenames:
with open(os.path.join(specs, spec_filename)) as f:
spec = json.load(f)
youtube_id = spec['audio_source']['youtube_id']
start_time = spec['audio_source']['start_time']
end_time = spec['audio_source']['end_time']
raw_audio_filenames = glob.glob(os.path.join(raw_audio, youtube_id + '.*'))
if len(raw_audio_filenames) == 0:
# No audio file found, skip.
continue
raw_audio_filename = raw_audio_filenames[0]
raw_audio_extension = os.path.splitext(raw_audio_filename)[1]
clip_filename = os.path.join(
output, CLIP_NAME_PATTERN.format(youtube_id, start_time, end_time) +
raw_audio_extension)
# Call ffmpeg to output the trimmed clip.
os.makedirs(os.path.dirname(clip_filename), exist_ok=True)
call1 = ['ffmpeg', '-loglevel', 'error', '-n',
'-ss', str(start_time), '-t', str(end_time - start_time),
'-i', raw_audio_filename]
if raw_audio_extension == 'ogg':
call2 = ['-codec:a', 'libvorbis', '-strict', 'experimental']
else:
call2 = []
call3 = [clip_filename]
process = subprocess.run(call1 + call2 + call3)
if process.returncode != 0:
print("Error: {} encountered by {}".format(
process.returncode, clip_filename))
else:
print(clip_filename)
评论列表
文章目录