Added argparse + trimaudio using pydub

This commit is contained in:
itec78 2021-11-18 16:47:56 +01:00
parent ca77b1ed17
commit e4060d01b0
9 changed files with 80 additions and 110 deletions

View file

@ -1,10 +0,0 @@
#!/usr/bin/env python3
#Aggiunge un file in playlist
import playlistalo
import sys
if __name__ == '__main__':
file = sys.argv[1]
user = sys.argv[2]
print (playlistalo.add(file, user))

View file

@ -1,10 +0,0 @@
#!/usr/bin/env python3
#Aggiunge un file in playlist
import playlistalo
import sys
if __name__ == '__main__':
url = sys.argv[1]
user = sys.argv[2]
print (playlistalo.addurl(url, user))

View file

@ -1,11 +0,0 @@
#!/usr/bin/env python3
#restituisce la playlist attuale
import playlistalo
if __name__ == '__main__':
#pl = playlistalo.listplaylist()
pl = playlistalo.listtot()
#print ('\n'.join([", ".join(x) for x in pl]))
print ('\n'.join([x[0] for x in pl]))

View file

@ -1,8 +0,0 @@
#!/usr/bin/env python3
#esegue il bot Mastodon
import playlistalo
if __name__ == '__main__':
playlistalo.mastodon_bot()

View file

@ -1,9 +0,0 @@
#!/usr/bin/env python3
#Fa partire il loop musicale
import playlistalo
if __name__ == '__main__':
playlistalo.playlocal()

View file

@ -1,9 +0,0 @@
#!/usr/bin/env python3
#Fa partire il loop musicale
import playlistalo
if __name__ == '__main__':
playlistalo.plaympd()

View file

@ -1,8 +0,0 @@
#!/usr/bin/env python3
#esegue il bot Telegram
import playlistalo
if __name__ == '__main__':
playlistalo.telegram_bot()

View file

@ -17,6 +17,7 @@ import random
import configparser
import hashlib
from urllib.parse import urlparse, parse_qs
import argparse
from musicpd import MPDClient
@ -114,10 +115,10 @@ def addurl(url, user = "-unknown-"):
#legge le info
with open(os.path.join("cache", id + ".json")) as infile:
j = json.load(infile)
title = normalizetext(j['title'])
track = normalizetext(j['track'])
artist = normalizetext(j['artist'])
album = normalizetext(j['album'])
title = normalizetext(j.get('title'))
track = normalizetext(j.get('track'))
artist = normalizetext(j.get('artist'))
album = normalizetext(j.get('album'))
filetemp = os.path.join("cache", id + ".m4a")
print ('id: %s' %(id))
print ('title: %s' %(title))
@ -200,7 +201,10 @@ def add(filetemp, user = "-unknown-", title = None, id = None, track = None, art
else:
print ('--- Converto ---')
print (fileout)
subprocess.call([scriptpath + "/trimaudio.sh", filetemp, fileout])
# subprocess.call([scriptpath + "/trimaudio.sh", filetemp, fileout])
trimaudio(filetemp, fileout)
print ('--- Fine ---')
if not os.path.isfile(fileout):
@ -647,6 +651,38 @@ def mpdadd(client, listlen):
def trimaudio(fin, fout):
from pydub import AudioSegment
from pydub.silence import split_on_silence
maxlen = 240
tolerance = 15
fade = 10
audio = AudioSegment.from_file(fin, "m4a")
print(audio.duration_seconds)
def match_target_amplitude(sound, target_dBFS):
change_in_dBFS = target_dBFS - sound.dBFS
return sound.apply_gain(change_in_dBFS)
#trim
audio = split_on_silence(audio, min_silence_len=3000, silence_thresh=-70.0, seek_step=100)[0]
print(audio.duration_seconds)
#fade
if (audio.duration_seconds > maxlen + tolerance):
audio = audio[:maxlen*1000].fade_out(fade*1000)
print(audio.duration_seconds)
#normalize
audio = match_target_amplitude(audio, -20.0)
audio.export(fout, format="mp4")
@ -657,6 +693,44 @@ def mpdadd(client, listlen):
if __name__ == '__main__':
print ("This is a package, use other commands to run it")
parser = argparse.ArgumentParser('Playlistalo')
subparsers = parser.add_subparsers(dest='command')
parser_add = subparsers.add_parser('add', help='Add song from file')
parser_add.add_argument('file', help='Song file')
parser_add.add_argument('-u', '--user', help='User that sends the file', default=None)
parser_addurl = subparsers.add_parser('addurl', help='Add song from url')
parser_addurl.add_argument('url', help='Song url')
parser_addurl.add_argument('-u', '--user', help='User that sends the file', default=None)
parser_addurl = subparsers.add_parser('list', help='List curent playlist')
parser_addurl = subparsers.add_parser('mastodon', help='Run Mastodon bot')
parser_addurl = subparsers.add_parser('telegram', help='Run Telegram bot')
parser_addurl = subparsers.add_parser('playlocal', help='Play songs locally')
parser_addurl = subparsers.add_parser('plaympd', help='Play songs using MPD')
args = parser.parse_args()
if args.command == 'add':
print(add(args.file, args.user))
elif args.command == 'addurl':
print(addurl(args.url, args.user))
elif args.command == 'list':
pl = listtot()
#print ('\n'.join([", ".join(x) for x in pl]))
print ('\n'.join([x[0] for x in pl]))
elif args.command == 'mastodon':
mastodon_bot()
elif args.command == 'telegram':
telegram_bot()
elif args.command == 'playlocal':
playlocal()
elif args.command == 'plaympd':
plaympd()
else:
parser.print_help()

View file

@ -1,39 +0,0 @@
#!/bin/sh
secmax=240 #max length
sectol=30 #tolerance
secfad=10 #fade
#wav is faster
temp1=$(mktemp /tmp/playlistalo.XXXXXXXX.wav)
temp2=$(mktemp /tmp/playlistalo.XXXXXXXX.wav)
temp3=$(mktemp /tmp/playlistalo.XXXXXXXX.wav)
tempx=$(mktemp /tmp/playlistalo.XXXXXXXX.m4a)
#step 1 rimuove il silenzio
echo "Rimuovo il silenzio"
ffmpeg -i "$1" -to $(($secmax + $sectol + 30)) -af "silenceremove=start_periods=1:start_duration=1:start_threshold=-60dB:detection=peak,aformat=dblp,areverse,silenceremove=start_periods=1:start_duration=1:start_threshold=-60dB:detection=peak,aformat=dblp,areverse" -v quiet -y $temp1
#step 2 tronca e sfuma
len=$(ffprobe -show_entries stream=duration -of default=noprint_wrappers=1:nokey=1 -v quiet $temp1)
len=${len%.*}
if [ $len -gt $(($secmax + $sectol)) ];
then
echo "Tronco e sfumo"
ffmpeg -i $temp1 -to $secmax -af "afade=t=out:st=$(($secmax - $secfad)):d=$secfad" -v quiet -y $temp2
rm $temp1
mv $temp2 $temp3
else
mv $temp1 $temp3
fi
#step3 normalizza
echo "Normalizzo"
#ffmpeg -i $temp3 -af loudnorm -v quiet -y $tempx
ffmpeg-normalize -q -f $temp3 -c:a aac -o $tempx
rm $temp3
mv $tempx "$2"