0

I'm writing an mp3 file condenser that turns multiple files into 1 large file. The idea is I point to a group of thousands of files such as Episode_001_00234523.mp3 and it condenses all the files that are episode 1 into a single mp3 and one that is episode 2 into another. I have tried every syntax I can think of in the command line and I feel like I'm following the instructions but I have just gotten error after error. What am I doing wrong? Or is there something in this script that is unlikely to work?

Thanks in advance for any help

Python Code:

import subprocess
from glob import glob
import os, sys
import re


media_path = "."
output_dir = sys.argv[1]
if (output_dir[0], output_dir[1]) == ('"','"'):
    output_dir = output_dir[1:-2]
anime_re = re.compile("(.*)\.\d+\.\d+\.\d+-\d\.\d+\.\d+\.\d+.mp3")
os.chdir(media_path)

def condense(files):
    files = sorted(files)
    filename = '"' + os.path.join(output_dir,os.path.basename(files[0][1])) + '.mp3"'
    files = ['"' + os.path.basename(x[0]) + '"' for x in files]
    subprocess.call(f"./mp3cat -o {filename} {' '.join(files)}",shell=True)

safe_get = lambda x, y: "" if not x else x[y]
audios = glob(os.path.join(media_path,"*.mp3"))
names = [x for x in list(set([safe_get(re.findall(anime_re,x),0)for x in audios])) if x]
for i in names:
    found = [(x,i) for x in audios if x.startswith(i)]
    condense(found)

Sources used

https://old.reddit.com/r/ajatt/comments/em1yjs/make_condensed_audio_files_from_all_your_subs2srs/

https://gist.github.com/CrystalBear45/9cbc7d5def39a0d95c50f98965141a58

Instructions to reproduce on windows

Import all your desired subs2srs decks into a dummy Anki profile

Open a Windows command prompt and enter the following

python path/to/main.py path/to/collection.media output/folder

And then it asks you to put in the executable for mp3cat so you would type in something like:

path/to/mp3cat.exe

The files should output into your specified output folder

Link to the original zip:

https://mega.nz/folder/VKQkWAhI#dRmHR31_xsxhylCD4rN1uw

What I typed into the command line to try to make it work:

python "E:/Users/Bradley Aidan Johnson/Documents/Sentence Mining/Immersion Tracks/Bleach/Individual Episodes/001 - Copy/main.py" "E:/Users/Bradley Aidan Johnson/Documents/Sentence Mining/Immersion Tracks/Bleach/Individual Episodes/001 - Copy" "output/folder"

Error when I only told it to execute the py file:

main.py", line 8, in <module>
    output_dir = sys.argv[1]
IndexError: list index out of range
Phoenix
  • 1,553
  • 2
  • 13
  • 29
  • Hi, and welcome to Stack Overflow! See if any of these solutions help you: https://stackoverflow.com/questions/2640971/windows-is-not-passing-command-line-arguments-to-python-programs-executed-from-t Your code looks correct, it seems like the problem happens somewhere between running your command and the python interpreter running your program. – Phoenix Jun 10 '20 at 19:00

1 Answers1

0

The line sys.argv[1] reads only one argument, you should try:

output_dir_1 = sys.argv[1]
output_dir_2 = sys.argv[2]
Roshin Raphel
  • 2,612
  • 4
  • 22
  • 40
  • thanks for the quick response, I tried editing the code as you said and it returned this error: main.py", line 10, in if (output_dir[0], output_dir[1]) == ('"','"'): NameError: name 'output_dir' is not defined – Bradley Aidan Johnson Jun 10 '20 at 20:48
  • You should change the name `output_dir` to `output_dir_1` or `output_dir_2` wherever applicable. – Roshin Raphel Jun 10 '20 at 20:51
  • I'm afraid I don't have anywhere near the level of python knowledge to get that done. output_dir is referred to 5 separate times and im not quite sure what any of this syntax means – Bradley Aidan Johnson Jun 10 '20 at 21:05