|
@@ -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()
|