Browse Source

Added display input switcher and music converters

master
haemka 3 years ago
parent
commit
4e2c7c39cb
3 changed files with 310 additions and 0 deletions
  1. +40
    -0
      ddc-switch-input.sh
  2. +89
    -0
      flac2mp3.sh
  3. +181
    -0
      flacmover.py

+ 40
- 0
ddc-switch-input.sh View File

@@ -0,0 +1,40 @@
#!/usr/bin/env bash

MON=1 # Primary monitor
DP="0x0f"
HDMI1="0x11"
HDMI2="0x12"

###
CURR_INPUT=""

function toggle {
get_input
if [[ ${CURR_INPUT} == ${DP} ]]; then
set_input ${HDMI1}
elif [[ ${CURR_INPUT} == ${HDMI1} ]]; then
set_input ${DP}
fi
}

function set_input {
ddcutil setvcp -d ${MON} 60 ${1}
}

function get_input {
CURR_INPUT="0$(ddcutil getvcp -d ${MON} 60 -t | cut -d' ' -f4)"
}

if [ $@ ]; then
for arg in "$@"; do
shift;
case $arg in
"--dp") set_input ${DP} ;;
"--hdmi1") set_input ${HDMI1} ;;
"--hdmi2") set_input ${HDMI2} ;;
"-h") echo "Usage: $0 [--dp|--hdmi1|--hdmi2]" ;;
esac
done
else
toggle
fi

+ 89
- 0
flac2mp3.sh View File

@@ -0,0 +1,89 @@
#!/usr/bin/env bash

DEFAULT_BASEDIR=$(pwd)
DEFAULT_CONVERTER="ffmpeg"
FLACDIR="flac"
MP3DIR="mp3"

while [[ $# -gt 0 ]]; do
opt="$1"
shift;
case "$opt" in
"-d"|"--dir")
BASEDIR="$1"
shift;
;;
"-c"|"--converter")
CONVERTER="$1"
shift;
;;
"-h"|"--help")
usage
;;
esac
done

if [ -z ${BASEDIR} ]; then
echo "No Basedir parameter given."
BASEDIR="${DEFAULT_BASEDIR}"
fi

if [ -z ${CONVERTER} ]; then
CONVERTER="${DEFAULT_CONVERTER}"
fi

usage() {
cat<<EOF
Usage: $0 [-d BASEDIR] [-c CONVERTER]

-d, --dir BASEDIR The root directory where to start to convert recursively.
-c, --converter CONVERTER The converter to use. Choices are "ffmpeg" or "libav"
-h, --help This usage dialog
EOF
exit
}

convert_to_mp3() {
NUM_FILES=$(find . -type f -iname '*.flac' | wc -l)
CURRENT=${NUM_FILES}
IFS=$'\n'; for FLACFILE in $(find ${BASEDIR}/${FLACDIR}/ -type f -iname '*.flac'); do
SRCPATH=${FLACFILE}
DSTPATH=$(echo ${FLACFILE} | sed "s|^${BASEDIR}/${FLACDIR}/|${BASEDIR}/${MP3DIR}/|g;s|\.flac$|\.mp3|g")
SRCDIR=$(dirname "${SRCPATH}")
DSTDIR=$(dirname "${DSTPATH}")

if [ ! -d "${DSTDIR}" ]; then
echo "Creating ${DSTDIR}."
mkdir -p "${DSTDIR}"
fi

if [ ! -e "${DSTDIR}/folder.jpg" ]; then
echo "Copying cover art into ${DSTDIR}."
cp "${SRCDIR}/folder.jpg" "${DSTDIR}/folder.jpg"
fi


if [ ! -e "${DSTPATH}" ]; then
echo "Converting file ${CURRENT} of ${NUM_FILES}: $(basename $FLACFILE) -> $(basename $DSTPATH)"
if [ ${CONVERTER} == "libav" ]; then
# Convert using libav
avconv -i "${SRCPATH}" -c:a libmp3lame -b:a 192k -map_metadata 0:g:0 "${DSTPATH}"
elif [ ${CONVERTER} == "ffmpeg" ]; then
# Convert using ffmpeg
ffmpeg -i "${SRCPATH}" -ab 192k -vcodec copy -map_metadata 0:g:0 -hide_banner -v 0 "${DSTPATH}"
fi
fi



let CURRENT-=1
done
}

echo "Using ${BASEDIR} as basedir"
echo " Therefore we are converting from ${BASEDIR}/${FLACDIR} to ${BASEDIR}/${MP3DIR}"
read -p "Press any key to continue... (CTRL+C to abort)" -n1 -s
cd ${BASEDIR}
convert_to_mp3

# vim: set ts=2 sw=2 tw=0 noet :

+ 181
- 0
flacmover.py View File

@@ -0,0 +1,181 @@
#!/usr/bin/env python3
# flacmover.py -i sourcedir -o -destdir -f formatstring

import sys, getopt, os, fnmatch
import shutil, re
from mutagen import File
from mutagen.flac import FLAC, Picture

def walk(root):
for root, dirnames, filenames in os.walk(root):
cover = None
for filename in fnmatch.filter(filenames, '*.flac'):
filepath = root + '/' + filename
print(filename)
tag = File(filepath)
ext = os.path.splitext(filename)[1]
directory, filename = buildpath(tag, ext)
new_path = outpath + directory + '/'
new_filepath = new_path + filename
new_coverpath = new_path + 'folder.jpg'
print(new_filepath)
copy(filepath, new_filepath)
if not os.path.isfile(new_coverpath):
cover = getcover(filepath)
if cover != None:
print('Writing Albumart to {}'.format(new_coverpath))
cdata = open(new_coverpath, 'wb')
cdata.write(cover.data)
cdata.close()

def getcover(filepath):
covers = FLAC(filepath).pictures
for cover in covers:
if cover.type == 3:
print(str(cover))
return(cover)
return None

def buildpath(tag, ext):
parts = filenameformat.split('%')
for index, part in enumerate(parts):
if part != '':
if part in tag:
if part == 'tracknumber':
parts[index] = tag[part][0].zfill(2)
else:
parts[index] = replacer(tag[part][0])
else:
parts[index] = part
else:
parts[index] = part
path = ''.join(parts) + ext
directory = '/'.join(path.split('/')[:-1])
filename = ''.join(path.split('/')[-1])

return directory, filename

def replacer(content):
table = {
ord(u'Å'): 'Aa',
ord(u'Å'): 'aa',
ord(u'Ä'): 'Ae',
ord(u'ä'): 'ae',
ord(u'Æ'): 'Ae',
ord(u'æ'): 'ae',
ord(u'Á'): 'A',
ord(u'á'): 'a',
ord(u'À'): 'A',
ord(u'à'): 'a',
ord(u'Ã'): 'a',
ord(u'ã'): 'a',
ord(u'Ć'): 'C',
ord(u'ć'): 'c',
ord(u'Ç'): 'C',
ord(u'ç'): 'c',
ord(u'¢'): 'c',
ord(u'Ð'): 'D',
ord(u'ð'): 'd',
ord(u'É'): 'E',
ord(u'é'): 'e',
ord(u'È'): 'E',
ord(u'è'): 'e',
ord(u'Ẽ'): 'E',
ord(u'ẽ'): 'e',
ord(u'Í'): 'I',
ord(u'í'): 'i',
ord(u'Í'): 'I',
ord(u'í'): 'i',
ord(u'Ì'): 'I',
ord(u'ì'): 'i',
ord(u'Ĩ'): 'I',
ord(u'ĩ'): 'i',
ord(u'ł'): 'l',
ord(u'Ñ'): 'N',
ord(u'ñ'): 'n',
ord(u'Ö'): 'Oe',
ord(u'ö'): 'oe',
ord(u'Ø'): 'Oe',
ord(u'ø'): 'oe',
ord(u'Œ'): 'Oe',
ord(u'œ'): 'oe',
ord(u'Ó'): 'O',
ord(u'ó'): 'o',
ord(u'Ò'): 'O',
ord(u'ò'): 'o',
ord(u'Õ'): 'O',
ord(u'õ'): 'o',
ord(u'Ü'): 'Ue',
ord(u'ü'): 'ue',
ord(u'Ú'): 'U',
ord(u'ú'): 'u',
ord(u'Ù'): 'U',
ord(u'ù'): 'u',
ord(u'Ũ'): 'U',
ord(u'ũ'): 'u',
ord(u'ß'): 'ss',
}
content = content.translate(table)
content = re.sub('[^\w\-_\. ]','_', content)
return content


def copy(srcfile, dstfile):
if not os.path.exists(os.path.dirname(dstfile)):
try:
os.makedirs(os.path.dirname(dstfile))
except OSError as exc: # Guard against race condition
if exc.errno != errno.EEXIST:
raise
print('Copying {} to {}'.format(srcfile, dstfile))
shutil.copy(srcfile, dstfile)

def usage(help = False):
if help:
print('This program is intended to copy FLAC music libraries into another directory using a defined structure.\n')
print('Copies all files within a source directory recursively to another directory and (re)names directories and filenames according to a specified format taken from the files Metadata.\nFor each folder in source an albumart image is exported from a FLAC file if possible.\n')
print('flacmover.py -i SRCDIR -o DESTDIR -f FORMATSTRING \n'
+ ' -i SRCDIR source root of files; defaults to current dir \n'
+ ' -o DESTDIR destination root of the renamed dirs and files \n'
+ ' -f FORMATSTRING any format of directory file structure \n'
+ ' with / as dir delimiter and %variable% for any variable \n'
+ ' found in FLAC file tags as used by mutagen. \n'
+ ' ex.: \n'
+ ' %albumartist%/%albumartist%.%originalyear%.%album%/%discnumber%.%tracknumber%.%artist%.%title%')
exit(1)


def main(argv):
global inpath, outpath, filenameformat
inpath = os.getcwd()
outpath = None
filenameformat = None

try:
opts, args = getopt.getopt(argv,'hi:o:f:',['input=','output=','format='])
except getopt.GetoptError:
usage()
for opt, arg in opts:
if opt == '-h':
usage(True)
elif opt in ('-i', '--input'):
inpath = arg
elif opt in ('-o', '--output'):
outpath = arg
elif opt in ('-f', '--format'):
filenameformat = arg
else:
usage()

print('Input path is {}'.format(inpath))

if outpath == None:
print('No output directory set!')
usage()
if not outpath.endswith('/'):
outpath = outpath + '/'

walk(inpath)

if __name__ == '__main__':
main(sys.argv[1:])

Loading…
Cancel
Save