Skip to content

Instantly share code, notes, and snippets.

@acidjunk
Last active September 17, 2022 15:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acidjunk/541162c15406b0cf95322be18bb27f74 to your computer and use it in GitHub Desktop.
Save acidjunk/541162c15406b0cf95322be18bb27f74 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2022 Formatics.nl <acidjunk@gmail.com>
# Crop all files in the script folder based on crop info loaded
# from an external "info.txt" which contains info regarding
# where to crop. Crop info format:
#
# 6 01:30-02:28
# 10 03:30-05:29
#
# File should be name like this:
# 6 - Your file name.mp3
# 10 - Second file name.mp3
#
# Usage: copy it to the folder with the .mp3 files and run it
# python crop.py
#
# It will create a folder "cropped" with the results.
#
# Enjoy!
# NEEDS LAME, SOX and libsox-fmt-mp3!!!!
import os
def delete_previous_run():
print("Deleting previous run files...")
os.system('rm -rf cropped')
def crop(file, start, end):
# OK here we go
# check to see if the list is empty, if not proceed
# Crop + fade in 40ms and fade out 1 sec
new_file = f"{start.replace(':','')}-{end.replace(':','')}-{file}"
cmd_string = f'sox "{file}" "cropped/{new_file}" trim {start} "={end}"'
# print("Running sox command: %s" % cmd_string)
os.system(cmd_string)
def split_crop_info(crop_info):
crop_info = crop_info.strip()
number, times = crop_info.split(" ")
start, end = times.split("-")
return number, start, end
def get_mp3_files_in_folder():
files = os.listdir(".")
return [file for file in files if file.endswith(".mp3")]
def get_file_for_number(number, files):
for file in files:
if file.startswith(f"{number} -"):
return file
print(f"Working in folder {os.getcwd()}")
delete_previous_run()
os.system("mkdir -p cropped")
with open("info.txt", encoding='utf-8') as f:
items = f.readlines()
print(f"ITEMS: {items}")
mp3_files = get_mp3_files_in_folder()
for item in items:
number, start, end = split_crop_info(item)
mp3_file = get_file_for_number(number, mp3_files)
print(f"Splitting file: {mp3_file} with start: {start} till end: {end}")
crop(mp3_file, start, end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment