CC-430: Audio normalization (Replaygain Support)

-Added support on media-monitor's side
This commit is contained in:
Martin Konecny 2012-07-05 21:54:44 -04:00
parent 1cbb0345b3
commit a687e48d80
7 changed files with 195 additions and 116 deletions

View File

@ -5,9 +5,6 @@ import time
import pyinotify
import shutil
from subprocess import Popen, PIPE
from api_clients import api_client
class AirtimeMediaMonitorBootstrap():
"""AirtimeMediaMonitorBootstrap constructor

View File

@ -1,13 +1,14 @@
# -*- coding: utf-8 -*-
import replay_gain
import os
import hashlib
import mutagen
import logging
import math
import re
import traceback
from api_clients import api_client
"""
list of supported easy tags in mutagen version 1.20
@ -123,8 +124,14 @@ class AirtimeMetadata:
self.logger.info("getting info from filepath %s", filepath)
try:
md = {}
replay_gain_val = replay_gain.calculate_replay_gain(filepath)
self.logger.info('ReplayGain calculated as %s for %s' % (replay_gain_val, filepath))
md['MDATA_KEY_REPLAYGAIN'] = replay_gain_val
try:
md5 = self.get_md5(filepath)
md['MDATA_KEY_MD5'] = md5
@ -135,6 +142,7 @@ class AirtimeMetadata:
self.logger.error("Exception %s", e)
return None
#check if file has any metadata
if file_info is None:
return None
@ -149,6 +157,7 @@ class AirtimeMetadata:
except Exception, e:
self.logger.error('Exception: %s', e)
self.logger.error("traceback: %s", traceback.format_exc())
if 'MDATA_KEY_TITLE' not in md:
#get rid of file extension from original name, name might have more than 1 '.' in it.
original_name = os.path.basename(filepath)
@ -165,8 +174,6 @@ class AirtimeMetadata:
pass
if isinstance(md['MDATA_KEY_TRACKNUMBER'], basestring):
match = re.search('^(\d*/\d*)?', md['MDATA_KEY_TRACKNUMBER'])
try:
md['MDATA_KEY_TRACKNUMBER'] = int(md['MDATA_KEY_TRACKNUMBER'].split("/")[0], 10)
except Exception, e:

View File

@ -8,13 +8,11 @@ import traceback
# For RabbitMQ
from kombu.connection import BrokerConnection
from kombu.messaging import Exchange, Queue, Consumer, Producer
from kombu.messaging import Exchange, Queue, Consumer
import pyinotify
from pyinotify import Notifier
#from api_clients import api_client
from api_clients import api_client
from airtimemetadata import AirtimeMetadata
class AirtimeNotifier(Notifier):
@ -153,7 +151,6 @@ class AirtimeNotifier(Notifier):
md.update(file_md)
else:
file_md = None
data = None
if (os.path.exists(filepath) and (mode == self.config.MODE_CREATE)):
if file_md is None:

View File

@ -9,7 +9,6 @@ import difflib
import traceback
from subprocess import Popen, PIPE
import pyinotify
from pyinotify import ProcessEvent
from airtimemetadata import AirtimeMetadata

View File

@ -10,7 +10,6 @@ import traceback
from subprocess import Popen, PIPE
from airtimemetadata import AirtimeMetadata
from api_clients import api_client
import pyinotify
class MediaMonitorCommon:
@ -75,7 +74,7 @@ class MediaMonitorCommon:
try:
return self.is_user_readable(item, 'www-data', 'www-data') \
and self.is_user_readable(item, 'pypo', 'pypo')
except Exception, e:
except Exception:
self.logger.warn(u"Failed to check owner/group/permissions for %s", item)
return False
@ -110,7 +109,7 @@ class MediaMonitorCommon:
else:
pathname = dirname
is_dir = True
except Exception, e:
except Exception:
#something went wrong while we were trying to make world readable.
return False
@ -273,7 +272,7 @@ class MediaMonitorCommon:
File name charset encoding is UTF-8.
"""
stdout = stdout.decode("UTF-8")
except Exception, e:
except Exception:
stdout = None
self.logger.error("Could not decode %s using UTF-8" % stdout)

View File

@ -0,0 +1,81 @@
from subprocess import Popen, PIPE
import re
import os
import sys
def get_process_output(command):
"""
Run subprocess and return stdout
"""
p = Popen(command, shell=True, stdout=PIPE)
return p.communicate()[0].strip()
def run_process(command):
"""
Run subprocess and return "return code"
"""
p = Popen(command, shell=True)
return os.waitpid(p.pid, 0)[1]
def get_mime_type(file_path):
"""
Attempts to get the mime type but will return prematurely if the process
takes longer than 5 seconds. Note that this function should only be called
for files which do not have a mp3/ogg/flac extension.
"""
return get_process_output("timeout 5 file -b --mime-type %s" % file_path)
def calculate_replay_gain(file_path):
"""
This function accepts files of type mp3/ogg/flac and returns a calculated ReplayGain value.
If the value cannot be calculated for some reason, then we default to 1.
TODO:
Currently some of the subprocesses called will actually insert metadata into the file itself,
which we do *not* want as this changes the file's hash. Need to make a copy of the file before
we run this function.
http://wiki.hydrogenaudio.org/index.php?title=ReplayGain_1.0_specification
"""
search = None
if re.search(r'mp3$', file_path, re.IGNORECASE) or get_mime_type(file_path) == "audio/mpeg":
if run_process("which mp3gain > /dev/null") == 0:
out = get_process_output('mp3gain -q "%s" 2> /dev/null' % file_path)
search = re.search(r'Recommended "Track" dB change: (.*)', out)
else:
print "mp3gain not found"
#Log warning
elif re.search(r'ogg$', file_path, re.IGNORECASE) or get_mime_type(file_path) == "application/ogg":
if run_process("which vorbisgain > /dev/null && which ogginfo > dev/null") == 0:
run_process('vorbisgain -q -f "%s" 2>/dev/null >/dev/null' % file_path)
out = get_process_output('ogginfo "%s"' % file_path)
search = re.search(r'REPLAYGAIN_TRACK_GAIN=(.*) dB', out)
else:
print "vorbisgain/ogginfo not found"
#Log warning
elif re.search(r'flac$', file_path, re.IGNORECASE) or get_mime_type(file_path) == "audio/x-flac":
if run_process("which metaflac > /dev/null") == 0:
out = get_process_output('metaflac --show-tag=REPLAYGAIN_TRACK_GAIN "%s"' % file_path)
search = re.search(r'REPLAYGAIN_TRACK_GAIN=(.*) dB', out)
else:
print "metaflac not found"
#Log warning
else:
pass
#Log unknown file type.
replay_gain = 1
if search:
matches = search.groups()
if len(matches) == 1:
replay_gain = matches[0]
return replay_gain
# Example of running from command line:
# python replay_gain.py /path/to/filename.mp3
if __name__ == "__main__":
print calculate_replay_gain(sys.argv[1])

View File

@ -1,6 +1,5 @@
# -*- coding: utf-8 -*-
from mediaconfig import AirtimeMediaConfig
import traceback
import os