Skip to content

Commit

Permalink
Meta data (#3)
Browse files Browse the repository at this point in the history
* Preserve meta-data - Feature request from u/Spyerx

* Preserve meta-data - Feature request from u/Spyerx

---------

Co-authored-by: HomeLabineer <steve@WDT-OFFICE-03>
  • Loading branch information
HomeLabineer and HomeLabineer authored Nov 5, 2023
1 parent 1960bd3 commit a52edbc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ This is a command-line tool for converting audio files from one format to anothe
- Selectable audio quality for output files (low, medium, high)
- Optional actions for original audio files after conversion (none, remove, or move)
- Customizable logging levels and log file path
- Preserve metadata from original audio files during conversion (can be disabled with `--no-preserve-metadata` flag)
- Status bar whilst running:
```bash
Converting: 37%|██████████████████████████████████████▍ | 787/2153 [07:51<13:23, 1.70it/s]
Expand Down Expand Up @@ -56,7 +57,7 @@ Linux/Ubuntu/apt:
```bash
usage: audio_converter.py [-h] -f FOLDER_PATH [-i {wma,flac,wav,m4a,ogg,aac,alac,opus,ape,aiff,all} [{wma,flac,wav,m4a,ogg,aac,alac,opus,ape,aiff,all} ...]] [-o {mp3,flac,wav,m4a,aac,alac,opus,ape,aiff}]
[-l LOG_FILE] [-a {none,remove,move}] [-w WMA_DESTINATION] [--overwrite] [--log-level LOG_LEVEL]
[-q {low,medium,high}]
[-q {low,medium,high}] [--no-preserve-metadata]

Convert audio files in a given folder and its subfolders.

Expand All @@ -79,6 +80,8 @@ optional arguments:
Logging level: debug, info (default), warning, error, or critical
-q {low,medium,high}, --audio-quality {low,medium,high}
Audio quality for output files (default: high)
--no-preserve-metadata
Do not preserve metadata from original files (default: preserve metadata)
```
---
Expand All @@ -95,6 +98,8 @@ optional arguments:
3. By default, the ThreadPoolExecutor uses the number of CPUs on the machine as the number of threads in the pool. You can explicitly set the number of threads by passing the `--max_workers` parameter. If you have 8 cores, try 4 or 6 to prevent maxing out all cores. Leaving this setting on default can max out all CPU cores easily as ffmpeg wants to work as quickly as possible.
4. The `--no-preserve-metadata` flag allows you to disable metadata preservation from the original files during conversion. By default, the script preserves metadata, but in cases where you want to discard this information, you can use this flag. Example usage: `--no-preserve-metadata`.
---
## Examples
Expand All @@ -111,6 +116,10 @@ optional arguments:
`python3 audio_converter.py --folder-path /path/to/folder --input-format wma flac m4a --output-format mp3`
4. Convert all WMA files in a folder to MP3 files with high quality, while not preserving metadata:
`python audio_converter.py -f /path/to/your/folder --no-preserve-metadata`
---
## Contributing
Expand Down
7 changes: 5 additions & 2 deletions audio_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def find_audio_files(dir_path, input_formats, output_format):

return audio_files

def convert_audio(input_file, output_format, input_format, audio_quality, overwrite):
def convert_audio(input_file, output_format, input_format, audio_quality, overwrite, preserve_metadata):
# Convert an audio file from one format to another
output_file = input_file[:-len(input_format)] + f'.{output_format}'

Expand All @@ -157,6 +157,8 @@ def convert_audio(input_file, output_format, input_format, audio_quality, overwr

codec = AUDIO_QUALITY[output_format]["codec"]
ffmpeg_options = f'-loglevel panic -y {AUDIO_QUALITY[output_format]["options"][audio_quality]} -acodec {codec}'
if preserve_metadata:
ffmpeg_options += ' -map_metadata 0'

ff = FFmpeg(inputs={input_file: None}, outputs={output_file: ffmpeg_options})
try:
Expand Down Expand Up @@ -209,7 +211,7 @@ def main(args):
with ThreadPoolExecutor(max_workers=args.max_workers) as executor:
futures = []
for input_file in audio_files:
futures.append(executor.submit(convert_audio, input_file, output_format, input_format, audio_quality, overwrite))
futures.append(executor.submit(convert_audio, input_file, output_format, input_format, audio_quality, overwrite, args.preserve_metadata))

# Iterate over the completed futures, handling the results
for future in tqdm(concurrent.futures.as_completed(futures), total=len(futures), desc="Converting"):
Expand Down Expand Up @@ -257,6 +259,7 @@ def main(args):
parser.add_argument('--log-level', default='info', type=str, help='Logging level: debug, info (default), warning, error, or critical')
parser.add_argument('-q', '--audio-quality', default='high', choices=['low', 'medium', 'high'], type=str, help='Audio quality for output files (default: high)')
parser.add_argument('--max-workers', default=None, type=int, help='Maximum number of worker threads to use for audio conversion (default: number of CPUs)')
parser.add_argument('--no-preserve-metadata', action='store_false', default=True, dest='preserve_metadata', help='Do not preserve metadata from original files (default: preserve metadata)')

args = parser.parse_args()

Expand Down

0 comments on commit a52edbc

Please sign in to comment.