Reputation: 65
I have a mp3 with a voice over "speech" and one with the background music "back_mus", and I want to combine them into one mp3 file. I tried running the code bellow, but I get this error "AttributeError: 'CompositeAudioClip' object has no attribute 'fps'". How do I get this to work? Thank You in advance.
speech = AudioFileClip(r"{}".format(cwd) + "/SpeechFolder/speech100.mp3")
back_music = AudioFileClip(r"{}".format(cwd) + "/back_mus.mp3")
back_music = back_music.subclip(0, int(speech.duration)) #I don't know if this line works, if it is wrong please tell me how to fix this, but you can pretty much disregard this part, I just tried to do this because the back_mus mp3 is pretty big, and I did not want to work with a 100 mb file every time, so I intended to make it the same length as the voice over mp3
final_clip = CompositeAudioClip([speech, back_music])
final_clip.write_audiofile(r"{}".format(cwd) + "/SpeechFolder/speech1000.mp3")
Full error:
Traceback (most recent call last):
File "c:\Users\TheD4\OneDrive\Desktop\New folder\Body.py", line 205, in body
final_clip.write_audiofile(r"{}".format(cwd) + "/SpeechFolder/speech1000.mp3")
File "<decorator-gen-45>", line 2, in write_audiofile
File "C:\Users\TheD4\AppData\Local\Programs\Python\Python39\lib\site-packages\moviepy\decorators.py", line 54, in requires_duration
return f(clip, *a, **k)
File "C:\Users\TheD4\AppData\Local\Programs\Python\Python39\lib\site-packages\moviepy\audio\AudioClip.py", line 192, in write_audiofile
if not self.fps:
AttributeError: 'CompositeAudioClip' object has no attribute 'fps'
Upvotes: 1
Views: 2642
Reputation: 134
**Edit ** I made a mistake in the images, change the audio fps to "44100", not "44800" like I demonstrated. The fps which works is 44100.
I too encountered this error, my solution is quite crude, but it works.
CompositeAudioClip class doesn't initialize an fps attribute. I have some ideas to explain this peculiarity, but I'm not certain.
One possible contender: If compositing audio clips of different fps values, the program relied once relied on an alternate way of standardizing the value.
Fix at a glance: I opened the moviepy program files, found the CompositeAudioClip class, and added the standard fps attribute.
Method
Locate the "Audioclip.py" file in the downloaded moviepy folder on your machine. The file path is usually given in the syntax error. If your using Pycharm and on windows, it will look similar to this:
C:\Users\Your_name\PycharmProjects\Your_Project\venv\Lib\site-packages\lib\site-packages\moviepy\audio\AudioClip.py"
Open the file the locate the CompositeAudioClip Class. (line 265):
Then initialize an fps value. I chose 48100hz as this is the standard for audio for video.
Now run your code!
This worked for me, I hope you found it useful.
However, this code has problems. Using a preset fps is a blanket treatment to every audio file, ignoring nuance that demands a more appropriate fps setting. If you have significantly higher or lower fps audio files to composite, manually edit the fps in the code to the desirable value.
Upvotes: 0
Reputation: 101
speech = AudioFileClip(r"{}".format(cwd) +"/SpeechFolder/speech100.mp3")
back_music = AudioFileClip(r"{}".format(cwd) + "/back_mus.mp3")
back_music = back_music.subclip(0, int(speech.duration))
final_clip = CompositeAudioClip([speech, back_music])
final_clip.write_audiofile(r"{}".format(cwd) +"/SpeechFolder/speech1000.mp3", fps = 16000)
you can invoke write_audiofile with fps parameter
Upvotes: 0
Reputation: 1
I'm facing the same problem. The answer is on github. Here is the code that helped me out:
import moviepy.editor as mpy
clip=mpy.VideoFileClip("sample.mp4")
subclip=mpy.concatenate_videoclips([clip.subclip((1,0),(2,0)),clip.subclip((3,0),(4,0)),clip.subclip((5,0),(6,0))])
aud = subclip.audio.set_fps(44100)
subclip = subclip.without_audio().set_audio(aud)
subclip.preview()
Upvotes: -1
Reputation:
It seems that you found a bug. I think composite clips don't get an fps automatically if the different clips have different FPSs. You can always set a fps, for instance with
newclip=clip.set_fps(25)
You can also modify AudioClip. Dirty hack while trying to trace the issue: in AudioClip.py, change:
if fps is None:
fps = self.fps
to
if fps is None:
fps = 44100
Upvotes: 0