padge

all-to

Jul 31st, 2025
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.77 KB | Source Code | 0 0
  1. #! /bin/python
  2.  
  3. import sys, argparse
  4. import os, subprocess
  5.  
  6. TEST = False
  7.  
  8. AUDIO_EXTENSIONS = {"aac", "flac", "mp3", "ogg", "opus", "wav"}
  9. CODEC_LIBS = {"aac": "aac", "flac": "flac", "mp3": "libmp3lame", "ogg": "libvorbis", "opus": "libopus", "wav": "wav"} #wav is wrong. I know it is. I'll fix it later (next time I want wav output).
  10. HELP_EPILOG="""
  11. """
  12.  
  13. WORK_DIR: str
  14. DEST_DIR: str
  15.  
  16. Codec = None
  17.  
  18. def get_args():
  19.     argparser = argparse.ArgumentParser(
  20.         formatter_class=argparse.RawDescriptionHelpFormatter,
  21.         description="Convert all audio files in folder to a specified type.",
  22.         epilog=HELP_EPILOG
  23.         )
  24.     argparser.add_argument("to", type=str, nargs=1, help="Codec to transcode into (file extension)")
  25.     argparser.add_argument("-f", action="store_true", help="Folderize output")
  26.     return argparser.parse_args()
  27.  
  28. if __name__ == "__main__":
  29.     WORK_DIR = os.getcwd()
  30.     DEST_DIR = WORK_DIR
  31.  
  32.     args = get_args()
  33.     if not args.to:
  34.         print("You must specify a codec (by file extension) to which to convert.")
  35.         sys.exit(1)
  36.     else:
  37.         Codec = args.to[0].lower()
  38.         AUDIO_EXTENSIONS.remove(Codec)
  39.  
  40.     if args.f:
  41.         output_folder = "%s.%s" % (os.path.split(WORK_DIR)[1], Codec)
  42.         output_fullpath = os.path.join(WORK_DIR, output_folder)
  43.         if os.path.exists(output_fullpath):
  44.             if os.path.isdir(output_fullpath):
  45.                 print("Output folder already exists.")
  46.             else:
  47.                 print("Output folder name exists, but is not a folder. Aborting.")
  48.                 sys.exit(1)
  49.         else:
  50.             print("Making the folder: %s." % output_fullpath)
  51.             if not TEST:
  52.                 os.mkdir(output_fullpath)
  53.             else:
  54.                 print("mkdir %s" % output_fullpath)
  55.  
  56.         DEST_DIR = output_fullpath
  57.  
  58.     files = os.listdir(WORK_DIR)
  59.     job_files = []
  60.     for f in files:
  61.         fbase, fext = os.path.splitext(f)
  62.         if fext[1:] in AUDIO_EXTENSIONS:
  63.             proc = subprocess.run(["ffprobe", os.path.join(WORK_DIR, f)], capture_output = True)
  64.  
  65.             if proc.stderr.decode("UTF-8").find("Audio:") != -1:
  66.                 job_files.append(f)
  67.             else:
  68.                 print("File \"%s\" didn't contain an audio stream. Skipping..." % f)
  69.                 continue
  70.     for f in job_files:
  71.         fq_input_name = os.path.join(WORK_DIR, f)
  72.         output_name = "%s.%s" % (os.path.splitext(f)[0], Codec)
  73.         fq_output_name = os.path.join(DEST_DIR, output_name)
  74.         cmd = [
  75.             "ffmpeg",
  76.             "-i", fq_input_name,
  77.             "-c:a", CODEC_LIBS[Codec],
  78.             fq_output_name
  79.         ]
  80.         if not TEST:
  81.             subprocess.run(cmd)
  82.         else:
  83.             print(command)
  84.  
Tags: music
Advertisement
Add Comment
Please, Sign In to add comment