PlayerModule.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. # coding: utf8
  2. import logging
  3. import os
  4. import subprocess
  5. from kalliope.core.Utils.FileManager import FileManager
  6. logging.basicConfig()
  7. logger = logging.getLogger("kalliope")
  8. class PlayerModule(object):
  9. """
  10. Mother class of Players.
  11. Ability to convert mp3 to wave format.
  12. """
  13. def __init__(self, **kwargs):
  14. # set parameter from what we receive from the settings
  15. self.convert = kwargs.get('convert_to_wav', True)
  16. @staticmethod
  17. def convert_mp3_to_wav(file_path_mp3):
  18. """
  19. PyAudio, AlsaPlayer, sounddevices do not support mp3 files
  20. MP3 files must be converted to a wave in order to be played
  21. This function assumes ffmpeg is available on the system
  22. :param file_path_mp3: the file path to convert from mp3 to wav
  23. """
  24. logger.debug("Converting mp3 file to wav file: %s" % file_path_mp3)
  25. fnull = open(os.devnull, 'w')
  26. # temp file
  27. tmp_file_wav = file_path_mp3 + ".wav"
  28. # Convert mp3 to wave
  29. subprocess.call(['avconv', '-y', '-i', file_path_mp3, tmp_file_wav],
  30. stdout=fnull, stderr=fnull)
  31. # remove the original file
  32. FileManager.remove_file(file_path_mp3)
  33. # rename the temp file with the same name as the original file
  34. os.rename(tmp_file_wav, file_path_mp3)