faker_file.providers.mp3_file package

Subpackages

Module contents

class faker_file.providers.mp3_file.Mp3FileProvider(generator: Any)[source]

Bases: BaseProvider, FileMixin

MP3 file provider.

Usage example:

from faker import Faker from faker_file.providers.mp3_file import Mp3FileProvider

FAKER = Faker() file = Mp3FileProvider(FAKER).mp3_file()

Usage example with options:

file = Mp3FileProvider(FAKER).mp3_file(

prefix=”zzz”, max_nb_chars=500,

)

Usage example with FileSystemStorage storage (for Django):

from django.conf import settings from faker_file.storages.filesystem import FileSystemStorage

file = Mp3FileProvider(FAKER).mp3_file(
storage=FileSystemStorage(

root_path=settings.MEDIA_ROOT, rel_path=”tmp”,

), prefix=”zzz”, max_nb_chars=500,

)

Default MP3 generator class is GttsMp3Generator which uses Google Text-to-Speech services to generate an MP3 file from given or randomly generated text. It does not require additional services to run and the only dependency here is the gtts package. You can however implement your own custom MP3 generator class and pass it to te mp3_file method in mp3_generator_cls argument instead of the default GttsMp3Generator.

Usage with custom MP3 generator class.

# Imaginary marytts Python library from marytts import MaryTTS

# Import BaseMp3Generator from faker_file.providers.base.mp3_generator import (

BaseMp3Generator,

)

# Define custom MP3 generator class MaryTtsMp3Generator(BaseMp3Generator):

locale: str = “cmu-rms-hsmm” voice: str = “en_US”

def handle_kwargs(self, **kwargs) -> None:

# Since it’s impossible to unify all TTS systems it’s allowed # to pass arbitrary arguments to the BaseMp3Generator # constructor. Each implementation class contains its own # additional tuning arguments. Check the source code of the # implemented MP3 generators as an example. if “locale” in kwargs:

self.locale = kwargs[“locale”]

if “voice” in kwargs:

self.voice = kwargs[“voice”]

def generate(self) -> bytes:

# Your implementation here. Note, that self.content # in this context is the text to make MP3 from. # self.generator would be the Faker or Generator # instance from which you could extract information on # active locale. # What comes below is pseudo implementation. mary_tts = MaryTTS(locale=self.locale, voice=self.voice) return mary_tts.synth_mp3(self.content)

# Generate MP3 file from random text file = Mp3FileProvider(FAKER).mp3_file(

mp3_generator_cls=MaryTtsMp3Generator,

)

extension: str = 'mp3'
mp3_file(storage: Optional[BaseStorage] = None, prefix: Optional[str] = None, max_nb_chars: int = DEFAULT_AUDIO_MAX_NB_CHARS, content: Optional[str] = None, mp3_generator_cls: Optional[Union[str, Type[BaseMp3Generator]]] = GttsMp3Generator, mp3_generator_kwargs: Optional[Dict[str, Any]] = None, raw: bool = True, **kwargs) BytesValue[source]
mp3_file(storage: Optional[BaseStorage] = None, prefix: Optional[str] = None, max_nb_chars: int = DEFAULT_AUDIO_MAX_NB_CHARS, content: Optional[str] = None, mp3_generator_cls: Optional[Union[str, Type[BaseMp3Generator]]] = GttsMp3Generator, mp3_generator_kwargs: Optional[Dict[str, Any]] = None, **kwargs) StringValue

Generate a MP3 file with random text.

Parameters:
  • storage – Storage. Defaults to FileSystemStorage.

  • prefix – File name prefix.

  • max_nb_chars – Max number of chars for the content.

  • content – File content. Might contain dynamic elements, which are then replaced by correspondent fixtures.

  • mp3_generator_cls – Mp3 generator class.

  • mp3_generator_kwargs – Mp3 generator kwargs.

  • raw – If set to True, return BytesValue (binary content of the file). Otherwise, return StringValue (path to the saved file).

Returns:

Relative path (from root directory) of the generated file or raw content of the file.