Drop screen as a dependency

This commit is contained in:
2024-03-24 17:37:53 -04:00
parent 3865e91517
commit c32b16598a
2 changed files with 26 additions and 20 deletions
+25 -19
View File
@@ -1,15 +1,15 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from argparse import ArgumentParser from argparse import ArgumentParser
from posixpath import join
from mutagen.flac import FLAC from mutagen.flac import FLAC
from os import cpu_count, getuid, listdir, path, stat, walk from os import cpu_count, getuid, listdir, path, stat, walk
from pathlib import Path from pathlib import Path
from shutil import copy from shutil import copy
from subprocess import check_output, DEVNULL, Popen from subprocess import DEVNULL, Popen
from sys import exit as s_exit from sys import exit as s_exit
from time import sleep from time import sleep
# scans source directory for music files, returns lists of album directories and their full paths # scans source directory for music files, returns lists of album directories and their full paths
def scan_source(_source): def scan_source(_source):
_artist_dirs, _album_dirs, _full_paths = [], [], [] _artist_dirs, _album_dirs, _full_paths = [], [], []
@@ -27,29 +27,32 @@ def scan_source(_source):
for _root, _directories, _files in walk(path.expanduser(_album)): for _root, _directories, _files in walk(path.expanduser(_album)):
for _filename in _files: for _filename in _files:
if _filename.lower().endswith('.flac') or _filename.lower().endswith('.mp3') or \ if _filename.lower().endswith('.flac') or _filename.lower().endswith('.mp3') or \
_filename.lower().endswith('.mp4') or _filename.lower().endswith('.m4a'): _filename.lower().endswith('.mp4') or _filename.lower().endswith('.m4a'):
_full_paths.append(f"{_album}/{_filename}") _full_paths.append(f"{_album}/{_filename}")
return _album_dirs, _full_paths return _album_dirs, _full_paths
# creates destination directory structure # creates destination directory structure
def create_destination(): def create_destination():
for _dir in source_directories[0]: for _dir in source_directories[0]:
Path(path.expanduser(_dir.replace(path.expanduser(source), path.expanduser(destination))))\ Path(path.expanduser(_dir.replace(path.expanduser(source), path.expanduser(destination))))\
.mkdir(0o700, parents=True, exist_ok=True) .mkdir(0o700, parents=True, exist_ok=True)
# counts how many ffmpeg processes are running under the current user # counts how many ffmpeg processes are running under the current user
def count_user_ffmpeg_processes(): def count_user_ffmpeg_processes():
user = getuid() _user = getuid()
proc_directory = '/proc' _proc_directory = '/proc'
running_processes = 0 _running_processes = 0
subdirectories = [d for d in listdir(proc_directory) if path.isdir(path.join(proc_directory, d))] _subdirectories = [_d for _d in listdir(_proc_directory) if path.isdir(path.join(_proc_directory, _d))]
for subdirectory in subdirectories: for _subdirectory in _subdirectories:
if subdirectory.isnumeric(): if _subdirectory.isnumeric():
if stat(proc_directory + '/' + subdirectory).st_uid == user: if stat(_proc_directory + '/' + _subdirectory).st_uid == _user:
with open (proc_directory + '/' + subdirectory + '/comm', 'r') as f: with open(_proc_directory + '/' + _subdirectory + '/cmdline', 'r') as _f:
if f.read().strip() == 'ffmpeg': if _f.read().startswith('ffmpeg'):
running_processes += 1 _running_processes += 1
return running_processes return _running_processes
def scan_destination_convert(): def scan_destination_convert():
_i, _i_total, _total_files = 0, 0, len(source_directories[1]) _i, _i_total, _total_files = 0, 0, len(source_directories[1])
@@ -65,7 +68,8 @@ def scan_destination_convert():
_file_d = _file.replace(path.expanduser(source), path.expanduser(destination))[:-5] + '.m4a' _file_d = _file.replace(path.expanduser(source), path.expanduser(destination))[:-5] + '.m4a'
_active_processes = count_user_ffmpeg_processes() _active_processes = count_user_ffmpeg_processes()
while _active_processes >= cpu_count(): while _active_processes >= cpu_count():
print(f"\nThere are already {_active_processes} processes running on your {cpu_count()} threads...\n") print(f"\nThere are already {_active_processes} processes running on your {cpu_count()} "
f"threads...\n")
sleep(1) sleep(1)
_active_processes = count_user_ffmpeg_processes() _active_processes = count_user_ffmpeg_processes()
_i += 1 _i += 1
@@ -110,22 +114,24 @@ def scan_destination_convert():
else: else:
_art_args = ['-vn'] _art_args = ['-vn']
# generate the appropriate ffmpeg command # generate the appropriate ffmpeg command
_cmd = ['ffmpeg', '-i', _file] + _art_args + ['-c:a', 'aac', '-b:a', '256k'] + _bake_args + _flac2pod_rg_tags + ['-aac_pns', '0', '-movflags', '+faststart', _file_d] _cmd = ['ffmpeg', '-i', _file] + _art_args + ['-c:a', 'aac', '-b:a', '256k'] + _bake_args +\
_flac2pod_rg_tags + ['-aac_pns', '0', '-movflags', '+faststart', _file_d]
if _rggain is not None: if _rggain is not None:
print(f"\u001b[38;5;0;48;5;15mGain adjustment: {_rggain}dB\u001b[0m") print(f"\u001b[38;5;0;48;5;15mGain adjustment: {_rggain}dB\u001b[0m")
else: else:
print(f"\u001b[38;5;0;48;5;88mNo ReplayGain data found!\u001b[0m") print('\u001b[38;5;0;48;5;88mNo ReplayGain data found!\u001b[0m')
if args.stopifnogain: if args.stopifnogain:
s_exit(1) s_exit(1)
Popen(['screen', '-DmS', f"flac2pod{_i}"] + _cmd) Popen(_cmd, stdout=DEVNULL, stderr=DEVNULL)
_active_processes = count_user_ffmpeg_processes() _active_processes = count_user_ffmpeg_processes()
while _active_processes != 0: while _active_processes != 0:
print('\nPlease wait for the remaining conversions to complete...\n') print(f"\nPlease wait for the remaining {_active_processes} conversions to complete...\n")
sleep(1) sleep(1)
_active_processes = count_user_ffmpeg_processes() _active_processes = count_user_ffmpeg_processes()
print('\niPod conversion complete!\n') print('\niPod conversion complete!\n')
s_exit(0) s_exit(0)
# argument parsing # argument parsing
if __name__ == "__main__": if __name__ == "__main__":
parser = ArgumentParser(description='Convert your existing FLAC library to be played on an iPod Classic.') parser = ArgumentParser(description='Convert your existing FLAC library to be played on an iPod Classic.')
+1 -1
View File
@@ -38,7 +38,7 @@ pkgdesc='Converts your FLAC library to be iPod-ready'
url='https://github.com/rwinkhart/flac2pod' url='https://github.com/rwinkhart/flac2pod'
arch=('any') arch=('any')
license=('GPL2') license=('GPL2')
depends=(ffmpeg flac python python-mutagen python-pillow screen) depends=(ffmpeg flac python python-mutagen python-pillow)
source=(\""$source"\") source=(\""$source"\")
sha512sums=('"$sha512"') sha512sums=('"$sha512"')