Created
June 17, 2020 10:01
-
-
Save davcri/6038f6a25ce40e9d6cef1cacf8d7734e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/python3 | |
""" | |
Converts flac files to opus, preserving audio quality but with smaller file size. | |
1. search recursively for *.flac files in the given path | |
2. convert the file in-place (using ffmpeg) | |
3. backup the original file into a backup directory | |
Dependencies: | |
- ffmpeg | |
- python3 | |
Generally they are both pre-installed on most linux distros. | |
Possible improvements: | |
1. parallelization | |
Example: | |
./flac2opus.py ~/my-album/ # the given path will be scanned recursively for *.flac files | |
before: | |
~/my-album/song1.flac | |
~/my-album/song2.flac | |
~/my-album/extra/extra-song.flac | |
after: | |
~/my-album/song1.opus | |
~/my-album/song1.opus | |
~/my-album/extra/extra-song.opus | |
~/my-album/backup/song1.flac | |
~/my-album/backup/song2.flac | |
~/my-album/backup/extra-song.flac | |
""" | |
import subprocess | |
import argparse | |
from sys import exit | |
from os import getcwd | |
from shutil import move | |
from pathlib import Path | |
# get arguments from CLI | |
parser = argparse.ArgumentParser( | |
description="Convert all the flac files in the specified path." | |
) | |
parser.add_argument('path', type=Path) | |
parser.add_argument('--bitrate', type=int, help="Output bitrate in kbps", default=180) | |
parser.add_argument('--backup-dir', type=Path, help="Backup directory") | |
args = parser.parse_args() | |
# set backup dir | |
if args.backup_dir: | |
BACKUP_DIR = args.backup_dir | |
else: | |
BACKUP_DIR = args.path / 'backup' | |
# get all flac files | |
files = args.path.glob("**/*.flac") # gets an iterator | |
files = list(files) # iterator to list | |
# quit if no flac file is found | |
if len(files) == 0: | |
print("Quitting...") | |
print("No flac file found in {}".format(getcwd())) | |
exit() | |
# list the files that will be converted | |
print("The following files are going to be converted:") | |
file_count = 0 | |
for i, f in enumerate(files): | |
print(' {index} - {filename}'.format(index = i + 1, filename = f)) | |
file_count = i + 1 | |
# tell user about the backup folder | |
print("A backup will be available at {}".format(BACKUP_DIR)) | |
# prompt | |
ans = input("Do you want to continue? [y/n]") | |
if ans.lower() != "y": | |
exit() | |
# make sure backup dir exists | |
BACKUP_DIR.mkdir(exist_ok=True) | |
# start converting files | |
print("Converting...") | |
# for each flac file | |
for idx, f in enumerate(files): | |
file_dir = f.parents[0] # absolute path to current file | |
output_filename = f.stem + ".opus" | |
# show progress to user | |
print(" [{current} / {total}] - {track}".format(current=idx + 1, total=file_count, track = file_dir/output_filename)) | |
# convert track with ffmpeg | |
subprocess.run(["ffmpeg", "-i", f, "-b:a", "{}k".format(args.bitrate), file_dir/output_filename, '-loglevel', 'quiet']) | |
# move original file to backup folder | |
move(f, BACKUP_DIR / f.name) | |
print("Done. Enjoy your music!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment