Audio and Subtitles¶
Inserting Audio¶
JAnim supports inserting audio and merging it into videos during export:
audio = Audio('audio.mp3')
self.play_audio(audio)
By default, audio will start playing at the current time; for specific details about playing audio, refer to the play_audio() method.
Synchronization Using Time Range Information¶
The play_audio() method also returns a TimeRange object, making this possible:
audio1 = Audio('audio1.mp3')
t = self.play_audio(audio1)
self.forward(t.duration)
txt = Text('This text is written as the audio plays')
audio2 = Audio('audio2.mp3')
t = self.play_audio(audio2)
self.play(Write(txt), duration=t.duration)
That is, by knowing the time range information of audio, such as the duration here, various flexible operations such as synchronizing audio with animations can be performed.
JAnim can also insert subtitles through the subtitle() method and synchronize them with audio:
self.forward()
audio = Audio('audio.mp3')
t = self.play_audio(audio)
self.subtitle('This is a subtitle', duration=t.duration)
self.forward_to(t.end)
self.forward()
For specific details about subtitles, refer to the subtitle() method.
Tip
subtitle() also returns a TimeRange object, providing the time range information of the subtitle.
Convenient Method for Inserting Subtitles and Their Audio¶
If you want to insert a subtitle and its audio, you can use the audio_and_subtitle() method, which is a wrapper around play_audio() and subtitle():
t = self.aas('audio.mp3', 'This is a subtitle')
self.forward(t.duration)
Hint
audio_and_subtitle() can be abbreviated as aas().
Important
The audio_and_subtitle() method automatically removes excessive silence before and after audio by default. You can pass clip=None to disable this, or use clip=(start, end) to manually determine the clipping range.