Introduction for the Random Music Video Generator
Are you a music artist looking to create unique and engaging music videos? Look no further! This small program uses the power of the Python programming language to generate random music videos by syncing them to the beats of your audio. You can achieve it by this Random Music Video Generator a.k.a MRvidGen Version 0.1.
How does MRvidGen works?
- The program will first fetch random videos from Pexels API based on the search query provided.
- It will then load the audio file and detect the beats using the librosa library.
- Then it will convert the audio file to mp3 format.
- Next, it will process each video and create subclips that align with the beats of the audio.
- Finally, it will combine all the subclips into a final video and synchronize it with the audio file.
- The final video will be saved in the same directory as the code with the name “final_video.mp4“
MRvidGen is still in development
- The program is still in development and I hope to host it on my site at some point so that you can just drag and drop your audio to get a cool video.
- The second things is that the automatic video editing process is not perfect yet so I am still working on that.
Prerequisites for MRvidGen
- Anaconda (Download Anaconda)
- Python 3.6
- A Pexels API key (https://www.pexels.com/api/)
- The following Python libraries installed: requests, json, random, librosa, soundfile, pydub, wave, moviepy (don’t worry if you are not a coder I will tell you in just a minute how to easily install them )
Installation
- Download and install Anaconda (Download Anaconda)
- In the Conda Terminal, install the dependencies (libraries) with 1 line
pip install requests json random librosa soundfile pydub wave moviepy
- Create MyVidGen.py file in the folder of your choice, open it with a text editor or anything else, and post this code into it:
import json
import random
import requests
import librosa
import soundfile as sf
import pydub
import wave
from moviepy.editor import VideoFileClip, concatenate_videoclips
# Fetch multiple random videos from Pexels API
response = requests.get("https://api.pexels.com/videos/search?query=chnage+this+part", headers={"Authorization": "YOUR_AUTH_KEY"})
response_json = json.loads(response.text)
video_urls = [v['video_files'][0]['link'] for v in response_json['videos']]
random.shuffle(video_urls)
# Load audio file and detect beats
audio, sr = librosa.load("YOUR_AUDIO.mp3", mono=True)
tempo, beat_frames = librosa.beat.beat_track(y=audio, sr=sr)
beat_times = librosa.frames_to_time(beat_frames, sr=sr)
# Convert audio data to mp3 file
# write the numpy array to a wave file
with wave.open("audio.wav", "w") as wav_file:
wav_file.setparams((1, 2, sr, 0, "NONE", "not compressed"))
wav_file.writeframes(audio.tobytes())
# Convert audio data to mp3 file
audio_mp3 = pydub.AudioSegment.from_file("audio.wav", format="wav")
audio_mp3.export("audio.mp3", format="mp3")
audio_mp3.export("audio.mp3", format="mp3")
# Prepare a list to store the subclips
subclips = []
#Process each video
for i, video_url in enumerate(video_urls):
# Load video file
video = VideoFileClip(video_url)
video_duration = 2
beat_times_filtered = [time for time in beat_times if time < video_duration]
for beat_time in beat_times_filtered:
subclip = video.subclip(0, beat_time)
subclips.append(subclip)
final_video = concatenate_videoclips(subclips)
final_audio = pydub.AudioSegment.from_file("YOUR_AUDIO.mp3", format="mp3")
final_audio.export("audio.mp3", format="mp3")
final_video.write_videofile("final_video.mp4", audio="audio.mp3", codec='libx264', audio_codec='aac')
#Creative Commons license Mykhaylo Aushev
- Go to that folder that you created through the Conda Terminal
cd "C:\Users\yourfolder\"
- Put your audio.mp3 in your folder
- In the code, replace
YOUR_AUTH_KEY
with your Pexels API key (that you get here when you register as a Pexels developer)
- Replace
YOUR_AUDIO.mp3
with the audio file title you would like to use.
- Replace
chnage+this+part
with a search query that you would like to use to fetch the videos. - Save the file (Shift +S)
- Run the code in your preferred Python environment.
Python MyVidGen.py
- Wait a couple of minutes and your video should be saved in the same folder as final_video.mp4 and you are done!
Troubleshooting MRvidGen
- Make sure that you have all the necessary libraries installed, and that the versions of the libraries are compatible with the code.
- Make sure that the audio file and the search query are correct.
- Make sure that you have a stable internet connection while running the code.
- Make sure that the Pexels API key is correct.
Conclusion MRvidGen, for now.
With this program, you can easily create fresh glitchy and engaging music videos that align with the beats of your audio. The program is easy to use and customizable, making it a great tool for any music artist looking to make their music videos (or make parts of the videos special). Give it a try and see what kind of music videos you can create!
Make sure to share this with your friends, send me what you make and tell me how to improve it and what you might like in the final version.
If you are looking for new Beats
Check out my beats if you are looking for new inspiration and new fresh pop sound!
My Beats
2 Responses
Очень круто вообще! Только ничего не понятно (ГЫ)
Спасибо! Скоро будет онлайн версия где просто кидаешь аудио и оно тебе даёт видео вместе с аудио. Но если хочешь уже сейчас попробывать то можещь попробывать пройти этот не большой туториал 🙂