Skip to content

Instantly share code, notes, and snippets.

@acidjunk
Last active September 17, 2022 15:20
Show Gist options
  • Select an option

  • Save acidjunk/541162c15406b0cf95322be18bb27f74 to your computer and use it in GitHub Desktop.

Select an option

Save acidjunk/541162c15406b0cf95322be18bb27f74 to your computer and use it in GitHub Desktop.

Revisions

  1. acidjunk revised this gist Sep 17, 2022. 1 changed file with 9 additions and 2 deletions.
    11 changes: 9 additions & 2 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,7 @@
    #!/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:
    @@ -11,9 +12,15 @@
    # 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!


    # NEED LAME, SOX and libsox-fmt-mp3!!!!
    # NEEDS LAME, SOX and libsox-fmt-mp3!!!!

    import os

  2. acidjunk revised this gist Sep 17, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    # Copyright (c) 2022 Formatics.nl <rene@formatics.nl>
    # 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:
  3. acidjunk revised this gist Sep 17, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    # Copyright (c) 2012 Formatics.nl <rene@formatics.nl>
    # Copyright (c) 2022 Formatics.nl <rene@formatics.nl>
    # 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:
  4. acidjunk revised this gist Sep 17, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@
    # 6 01:30-02:28
    # 10 03:30-05:29
    #
    # File should be name liek this:
    # File should be name like this:
    # 6 - Your file name.mp3
    # 10 - Second file name.mp3

  5. acidjunk revised this gist Sep 17, 2022. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,11 @@
    # where to crop. Crop info format:
    #
    # 6 01:30-02:28
    # 10 03:30-05:29
    # 10 03:30-05:29
    #
    # File should be name liek this:
    # 6 - Your file name.mp3
    # 10 - Second file name.mp3


    # NEED LAME, SOX and libsox-fmt-mp3!!!!
  6. acidjunk created this gist Sep 17, 2022.
    63 changes: 63 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,63 @@
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    # Copyright (c) 2012 Formatics.nl <rene@formatics.nl>
    # 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


    # NEED 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)