eorzea-songbook/build_clips.py
2023-06-04 12:52:50 +10:00

74 lines
1.5 KiB
Python

import sqlite3
import os
import glob
import shutil
import sox
shutil.rmtree('clips')
os.mkdir('clips')
db = sqlite3.connect("file:songbook.sqlite?mode=ro", uri=True)
cur = db.cursor()
for song, motif, start, duration in db.execute('''
select
song_id, motif_id, start_ms, duration_ms
from
clip'''):
album, track = db.execute('''
select
album_id, track
from
song_album
where
song_id = ?
order by
album_id desc limit 1''', (song,)).fetchone()
# get source path
album_folder = f'{album - 1:02}. *'
song_file = f'{track:03}. *.flac'
source = glob.glob(f"/mnt/petal/media/Audio/ffxiv/{album_folder}/{song_file}")[0]
print(source)
# get clip filename
destination = f'clips/{song:04}-{motif:03}.flac'
print(destination)
source_duration = sox.file_info.duration(source)
# trim and fade start
if start < 2:
trim_start = 0
fade_in = 0
else:
trim_start = start - 2
fade_in = 2
# trim and fade end (to be continued)
if source_duration - (start + duration ) < 2:
trim_end = None
fade_out = 0
else:
trim_end = start + duration + 2
fade_out = 2
# call sox
tf = sox.Transformer()
tf.trim(trim_start, trim_end)
tf.fade(fade_in, fade_out, 't')
sox_status = tf.build_file(source, destination, return_output=True)
# error check
if sox_status != (0, '', ''):
print(sox_status)