Created
May 12, 2024 17:42
-
-
Save AlexGascon/b797c3351a6831e3734425a74386f49f to your computer and use it in GitHub Desktop.
Python script to split an audio file into 29-minute fragments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
import json | |
import shlex | |
import subprocess | |
import sys | |
FFMPEG_COMMAND = 'ffmpeg -i "{cap_name}" -ss {start_time} -t 00:29:00 -acodec copy "{cap_name}_part_{number}.{extension}"' | |
DESIRED_AUDIO_FRAGMENT_LENGTH_SECONDS = 29 * 60 # 29 minutes | |
# Validate input | |
if len(sys.argv) != 2: | |
print(f"Incorrect number of arguments. The arguments passed are: {sys.argv}") | |
sys.exit(-1) | |
full_filename = sys.argv[1] | |
filename, extension = full_filename.split(".") | |
# Find audio duration | |
ffprobe_cmd = [ | |
'ffprobe', | |
'-v', 'error', | |
'-show_entries', 'format=duration', | |
'-of', 'json', | |
full_filename | |
] | |
print(f"COMMAND: {ffprobe_cmd}") | |
process = subprocess.run(ffprobe_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) | |
output = process.stdout | |
if output: | |
audio_info = json.loads(output) | |
duration = float(audio_info['format']['duration']) | |
print(f"DURATION: {duration}") | |
else: | |
print("Could not retrieve audio file information") | |
sys.exit(1) | |
# Split audio | |
number_of_fragments_needed = int(duration // DESIRED_AUDIO_FRAGMENT_LENGTH_SECONDS) + 1 | |
print(f"FRAGMENTS: {number_of_fragments_needed}") | |
for i in range(number_of_fragments_needed): | |
start_time = str(datetime.timedelta(seconds=i * DESIRED_AUDIO_FRAGMENT_LENGTH_SECONDS)) | |
command_str = FFMPEG_COMMAND.format(cap_name=full_filename, start_time=start_time, number=str(i), extension=extension) | |
# Splitting in a format that will work for bash, e.g. without splitting quoted args | |
command = shlex.split(command_str) | |
print(f"EXECUTING: {command}") | |
subprocess.run(command) | |
print("COMPLETED!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment