I'm trying to write a program that takes a bunch of videos from a folder, compiles them, and then takes another bunch of audio files from a folder and concatenates them to then overlay the combined audio on top of the final video (hope that makes sense). I'm able to process the videos into one final output video, but I keep running into trouble combining the audio. Below I've provided two small sections of code pertaining to audio comp, I'm using moviepy to process the videos and attempting to use it for audio as well.
songDirectory = './songs/'
songList = []
songs = []
audioclip=''
def makeCompilation():
for filename in os.listdir(songDirectory):
f = os.path.join(songDirectory, filename)
if os.path.isfile(f) and filename.endswith(".mp3"):
audioclip = moviepy.editor.AudioFileClip(f)
songList.append(filename)
for song in os.listdir(songDirectory):
c = os.path.join(songDirectory, song)
audio_clips = moviepy.editor.AudioFileClip(c)
audio_output = moviepy.editor.concatenate_audioclips(audio_clips)
finalClip = concatenate_videoclips(videos, method="compose")
final_duration = finalClip.duration
final_audio_output = audio_output.set_duration(final_duration)
final_output= finalClip.set_audio(final_audio_output).fx(afx.audio_fadein, 3.0)
audio_path = "./songs/tempaudiofile.m4a"
#print(description)
# Create compilation
final_output.write_videofile(outputFile, threads=8, temp_audiofile=audio_path, remove_temp=True, codec="libx264", audio_codec="aac")
return description
The program appeared to be able to find the audio directory, but I needed to be able to use os.path.join(songDirectory, filename) to point directly to each mp3 file so I messed around with it until I got the above code. And when I attempted to iterate through songList, I, as expected, got an error saying that 'str' wasn't iterable, and other errors saying that 'str' has no attribute 'duration'. Essentially, all I need it to do is iterate though the input folder and combine the files by whatever means. Code currently returns the output:
Free Fredobagz x Aflacko x Flint type beat - 'Default_Dance' [prod. kah]-jro0v6ogZ0Y.mp4
225.05
Total Length: 225.05
225.05
Traceback (most recent call last):
File "/Users/daddyK/Desktop/comp_ bot/make_compilation3.py", line 127, in <module>
makeCompilation(path = "./videos/",
File "/Users/daddyK/Desktop/comp_ bot/make_compilation3.py", line 110, in makeCompilation
audio_output = moviepy.editor.concatenate_audioclips(audio_clips)
File "/Users/daddyK/Library/Python/3.10/lib/python/site-packages/moviepy/audio/AudioClip.py", line 315, in concatenate_audioclips
durations = [c.duration for c in clips]
TypeError: 'AudioFileClip' object is not iterable
At this point I'm a bit stumped, so if anybody could offer some insight as to what I should do to resolve the error and/or if I'm headed in the right direction code-wise I'd greatly appreciate it! Sorry if the code doesn't make any sense I'll post the whole .py file if needed