|
| 1 | +""" |
| 2 | +Python Audio Converter |
| 3 | +
|
| 4 | +Programmed by: Paramon yevstigneyev |
| 5 | +Programed in: Python 3.9.7 (64-Bit) |
| 6 | +
|
| 7 | +Description: |
| 8 | +This is a Python Program, that can convert Audio files into any other common |
| 9 | +format (MP2, MP3, M4A, OGG, FLAC, WAV) you want to. |
| 10 | +""" |
| 11 | + |
| 12 | +# Used for checking if the audio file does exist, |
| 13 | +# and to remove the old file from the user's system. |
| 14 | +import os |
| 15 | +# Sued for command line arguments when the user runs the file |
| 16 | +import argparse |
| 17 | + |
| 18 | + # Used for converting the audio file into a new format |
| 19 | +import convert |
| 20 | + |
| 21 | +# Makes a parser to |
| 22 | +parser = argparse.ArgumentParser(description="A Python Audio Converter program") |
| 23 | + |
| 24 | +# Adds a '--file' argument for the use to specify the existing audio file |
| 25 | +parser.add_argument("--file", "--file", help="Name of the audio file to convert.") |
| 26 | +# Adds a '--new-format' argument, for the user to specify an audio format they want to convert it into. |
| 27 | +parser.add_argument("--new-format", "--new_format", help="Specify new audio file format.") |
| 28 | + |
| 29 | +# Adds a '--list-formats' argument, for the user to see the local files and audio file formats. |
| 30 | +parser.add_argument("--list","--list", help="Lists audio file formats or local audio files.") |
| 31 | + |
| 32 | +args = parser.parse_args() |
| 33 | + |
| 34 | +def convert_file(): |
| 35 | + if args.list != "" and args.list != None: |
| 36 | + |
| 37 | + if args.list == "files": |
| 38 | + print("\nLocal Files:") |
| 39 | + |
| 40 | + local_files = os.listdir() |
| 41 | + |
| 42 | + for audio_files in local_files: |
| 43 | + |
| 44 | + if "m4a" in audio_files: |
| 45 | + print(audio_files) |
| 46 | + |
| 47 | + elif "mp2" in audio_files: |
| 48 | + print(audio_files) |
| 49 | + |
| 50 | + elif "mp3" in audio_files: |
| 51 | + print(audio_files) |
| 52 | + |
| 53 | + elif "ogg" in audio_files: |
| 54 | + print(audio_files) |
| 55 | + |
| 56 | + elif "flac" in audio_files: |
| 57 | + print(audio_files) |
| 58 | + |
| 59 | + elif "wav" in audio_files: |
| 60 | + print(audio_files) |
| 61 | + |
| 62 | + else: |
| 63 | + pass |
| 64 | + |
| 65 | + print() |
| 66 | + |
| 67 | + elif args.list == "formats": |
| 68 | + audio_formats = ["flac", "mp2","mp3", "m4a", "ogg", "wav"] |
| 69 | + print("\nAudio file formats:") |
| 70 | + |
| 71 | + for formats in audio_formats: |
| 72 | + print(formats) |
| 73 | + |
| 74 | + print() |
| 75 | + |
| 76 | + else: |
| 77 | + pass |
| 78 | + |
| 79 | + elif args.file != "" and args.file != None: |
| 80 | + # If the user enters a file that does exist in the local directory, |
| 81 | + # then it will |
| 82 | + if os.path.isfile(args.file): |
| 83 | + |
| 84 | + # If the user enter 'mp3' as a new file format, then it will convert |
| 85 | + # the given file into an mp3 file. |
| 86 | + if args.new_format == "mp3": |
| 87 | + convert.audio_file(args.file, args.new_format) |
| 88 | + |
| 89 | + # If the user enter 'm4a' as a new file format, then it will convert |
| 90 | + # the given file into an m4a file. |
| 91 | + elif args.new_format == "m4a": |
| 92 | + convert.audio_file(args.file, args.new_format) |
| 93 | + |
| 94 | + # If the user enter 'mp2' as a new file format, then it will convert |
| 95 | + # the given file into an mp2 file. |
| 96 | + elif args.new_format == "mp2": |
| 97 | + convert.audio_file(args.file, args.new_format) |
| 98 | + |
| 99 | + # If the user enter 'flac' as a new file format, then it will convert |
| 100 | + # the given file into an flac file. |
| 101 | + elif args.new_format == "flac": |
| 102 | + convert.audio_file(args.file, args.new_format) |
| 103 | + |
| 104 | + # If the user enter 'ogg' as a new file format, then it will convert |
| 105 | + # the given file into an ogg file. |
| 106 | + elif args.new_format == "ogg": |
| 107 | + convert.audio_file(args.file, args.new_format) |
| 108 | + |
| 109 | + # If the user enter 'wav' as a new file format, then it will convert |
| 110 | + # the given file into an wav file. |
| 111 | + elif args.new_format == "wav": |
| 112 | + convert.audio_file(args.file, args.new_format) |
| 113 | + |
| 114 | + # If the user enters an invalid audio format, then it will |
| 115 | + # tell the user that it was an invalid format. |
| 116 | + else: |
| 117 | + print("\nInvalid file format!\n") |
| 118 | + |
| 119 | + # If the user enters a file name that does not exist, |
| 120 | + # or can not be found. It will show that the file was not found. |
| 121 | + else: |
| 122 | + print(f"\n'{args.file}' not found!\n") |
| 123 | + |
| 124 | +try: |
| 125 | + convert_file() |
| 126 | + |
| 127 | +# If the "KeyboardInterrupt" exception is raised, |
| 128 | +# then this will prevent the program from raaising this exception. |
| 129 | +except KeyboardInterrupt: |
| 130 | + pass |
| 131 | + |
| 132 | +# If any other excpetion is raised then this will |
| 133 | +# handle the excpetion when an exception is raised. |
| 134 | +except: |
| 135 | + convert_file() |
0 commit comments