Manim关于声音的操作。
1. 添加声音
添加声音,调用Scene.add_sound函数。
# This method is used to add a sound to the animation.
add_sound(sound_file, time_offset=0, gain=None, **kwargs) # gain (float) – Amplification of the sound.
2. 查看音频多长
查看音频多长,配合Manim动画等待时间,了解到有多种库可以对音频进行操作,我最后用的是mutagen。
(1)mutagen
# Mutagen is a Python module to handle audio metadata.
pip install mutagen
# example
from mutagen.wave import WAVE
from mutagen.mp3 import MP3
audio = WAVE('test.wav')
duration = audio.info.length
卸载mutagen,pip uninstall mutagen
:
D:\workspace\manim> pip uninstall mutagen
Found existing installation: mutagen 1.45.1
Uninstalling mutagen-1.45.1:
Would remove:
c:\anaconda3\lib\site-packages\mutagen-1.45.1.dist-info\*
c:\anaconda3\lib\site-packages\mutagen\*
c:\anaconda3\scripts\mid3cp.exe
c:\anaconda3\scripts\mid3iconv.exe
c:\anaconda3\scripts\mid3v2.exe
c:\anaconda3\scripts\moggsplit.exe
c:\anaconda3\scripts\mutagen-inspect.exe
c:\anaconda3\scripts\mutagen-pony.exe
c:\anaconda3\share\man\man1\mid3cp.1
c:\anaconda3\share\man\man1\mid3iconv.1
c:\anaconda3\share\man\man1\mid3v2.1
c:\anaconda3\share\man\man1\moggsplit.1
c:\anaconda3\share\man\man1\mutagen-inspect.1
c:\anaconda3\share\man\man1\mutagen-pony.1
Proceed (y/n)? y
Successfully uninstalled mutagen-1.45.1
(2)scipy.io
from scipy.io import wavfile
sample_rate, data = wavfile.read('test.wav')
duration = len(data) / sample_rate # returns duration in floats
我自己用pyttsx3
生成几个声音,长度不同,然而,用上述方法得到的结果却是一样,采样率为22050
,长度为26.75
。
(3)pydub
from pydub import AudioSegment
song = AudioSegment.from_mp3("test.mp3")
参考资料:
[1] How to get the duration of audio in Python? - GeeksforGeeks