简体   繁体   中英

How to combine 2 audio mp3s in moviepy?

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'

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

I'm facing the same problem. The answer is on github . Here is the code that helped me out. enter code here import moviepy.editor as mpy

enter code here clip=mpy.VideoFileClip("sample.mp4")

enter code here subclip=mpy.concatenate_videoclips([clip.subclip((1,0), enter code here (2,0)),clip.subclip((3,0),(4,0)),clip.subclip((5,0),(6,0))])

enter code here aud = subclip.audio.set_fps(44100) enter code here subclip = subclip.without_audio().set_audio(aud) enter code here subclip.preview()

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

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):

CompositeAudioClass 及其构造函数

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM