vidmuse
Browse files- README.md +63 -0
- video_processor.py +8 -6
README.md
CHANGED
|
@@ -1,3 +1,66 @@
|
|
| 1 |
---
|
| 2 |
license: cc-by-4.0
|
| 3 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
license: cc-by-4.0
|
| 3 |
---
|
| 4 |
+
|
| 5 |
+
# VidMuse
|
| 6 |
+
|
| 7 |
+
VidMuse is a framework designed for generating high-fidelity music that is acoustically and semantically aligned with video content.
|
| 8 |
+
|
| 9 |
+
## Usage
|
| 10 |
+
|
| 11 |
+
1. First install the [`VidMuse` library](git+https://github.com/ZeyueT/VidMuse)
|
| 12 |
+
```
|
| 13 |
+
pip install git+https://github.com/ZeyueT/VidMuse.git
|
| 14 |
+
```
|
| 15 |
+
|
| 16 |
+
2. Install ffmpeg:
|
| 17 |
+
Install ffmpeg:
|
| 18 |
+
```bash
|
| 19 |
+
sudo apt-get install ffmpeg
|
| 20 |
+
# Or if you are using Anaconda or Miniconda
|
| 21 |
+
conda install "ffmpeg<5" -c conda-forge
|
| 22 |
+
```
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
3. Run the following Python code:
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
```py
|
| 29 |
+
from video_processor import VideoProcessor, merge_video_audio
|
| 30 |
+
from audiocraft.models import VidMuse
|
| 31 |
+
import scipy
|
| 32 |
+
|
| 33 |
+
# Path to the video
|
| 34 |
+
video_path = '/data/zeyuet/project/VidMuse/VidMuse-hf/dataset/example/infer/sample.mp4'
|
| 35 |
+
# Initialize the video processor
|
| 36 |
+
processor = VideoProcessor()
|
| 37 |
+
# Process the video to obtain tensors and duration
|
| 38 |
+
local_video_tensor, global_video_tensor, duration = processor.process(video_path)
|
| 39 |
+
|
| 40 |
+
progress = True
|
| 41 |
+
USE_DIFFUSION = False
|
| 42 |
+
|
| 43 |
+
# Load the pre-trained VidMuse model
|
| 44 |
+
MODEL = VidMuse.get_pretrained('Zeyue7/VidMuse')
|
| 45 |
+
# Set generation parameters for the model based on video duration
|
| 46 |
+
MODEL.set_generation_params(duration=duration)
|
| 47 |
+
|
| 48 |
+
try:
|
| 49 |
+
# Generate outputs using the model
|
| 50 |
+
outputs = MODEL.generate([local_video_tensor, global_video_tensor], progress=progress, return_tokens=USE_DIFFUSION)
|
| 51 |
+
except RuntimeError as e:
|
| 52 |
+
print(e)
|
| 53 |
+
|
| 54 |
+
# Detach outputs from the computation graph and convert to CPU float tensor
|
| 55 |
+
outputs = outputs.detach().cpu().float()
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
sampling_rate = 32000
|
| 59 |
+
output_wav_path = "vidmuse_out.wav"
|
| 60 |
+
# Write the output audio data to a WAV file
|
| 61 |
+
scipy.io.wavfile.write(output_wav_path, rate=sampling_rate, data=outputs[0, 0].numpy())
|
| 62 |
+
|
| 63 |
+
output_video_path = "output_video.mp4"
|
| 64 |
+
# Merge the original video with the generated music
|
| 65 |
+
merge_video_audio(video_path, output_wav_path, output_video_path)
|
| 66 |
+
```
|
video_processor.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
from moviepy.editor import VideoFileClip
|
| 2 |
import torch
|
| 3 |
from decord import VideoReader, cpu
|
| 4 |
import math
|
|
@@ -31,7 +31,6 @@ class VideoProcessor:
|
|
| 31 |
video_tensor = torch.cat((video_tensor, last_frame.repeat(1, repeat_times, 1, 1)), dim=1)
|
| 32 |
return video_tensor
|
| 33 |
|
| 34 |
-
|
| 35 |
def video_read_global(self, filepath, seek_time=0., duration=-1, target_fps=2, global_mode='average', global_num_frames=32):
|
| 36 |
vr = VideoReader(filepath, ctx=cpu(0))
|
| 37 |
fps = vr.get_avg_fps()
|
|
@@ -67,13 +66,16 @@ class VideoProcessor:
|
|
| 67 |
assert global_video_tensor.shape[1] == global_num_frames, f"the shape of global_video_tensor is {global_video_tensor.shape}"
|
| 68 |
return local_video_tensor, global_video_tensor
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
| 72 |
def process(self, video_path, target_fps=2, global_mode='average', global_num_frames=32):
|
| 73 |
duration = self.get_video_duration(video_path)
|
| 74 |
if duration is None:
|
| 75 |
raise ValueError("Invalid video path or video file.")
|
| 76 |
-
|
| 77 |
local_video_tensor, global_video_tensor = self.video_read_global(video_path, duration=duration, target_fps=target_fps, global_mode=global_mode, global_num_frames=global_num_frames)
|
| 78 |
-
|
| 79 |
return local_video_tensor, global_video_tensor, duration
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from moviepy.editor import VideoFileClip, AudioFileClip
|
| 2 |
import torch
|
| 3 |
from decord import VideoReader, cpu
|
| 4 |
import math
|
|
|
|
| 31 |
video_tensor = torch.cat((video_tensor, last_frame.repeat(1, repeat_times, 1, 1)), dim=1)
|
| 32 |
return video_tensor
|
| 33 |
|
|
|
|
| 34 |
def video_read_global(self, filepath, seek_time=0., duration=-1, target_fps=2, global_mode='average', global_num_frames=32):
|
| 35 |
vr = VideoReader(filepath, ctx=cpu(0))
|
| 36 |
fps = vr.get_avg_fps()
|
|
|
|
| 66 |
assert global_video_tensor.shape[1] == global_num_frames, f"the shape of global_video_tensor is {global_video_tensor.shape}"
|
| 67 |
return local_video_tensor, global_video_tensor
|
| 68 |
|
|
|
|
|
|
|
| 69 |
def process(self, video_path, target_fps=2, global_mode='average', global_num_frames=32):
|
| 70 |
duration = self.get_video_duration(video_path)
|
| 71 |
if duration is None:
|
| 72 |
raise ValueError("Invalid video path or video file.")
|
|
|
|
| 73 |
local_video_tensor, global_video_tensor = self.video_read_global(video_path, duration=duration, target_fps=target_fps, global_mode=global_mode, global_num_frames=global_num_frames)
|
|
|
|
| 74 |
return local_video_tensor, global_video_tensor, duration
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
def merge_video_audio(video_path, audio_path, output_path):
|
| 78 |
+
video = VideoFileClip(video_path).without_audio()
|
| 79 |
+
audio = AudioFileClip(audio_path)
|
| 80 |
+
final_video = video.set_audio(audio)
|
| 81 |
+
final_video.write_videofile(output_path, codec='libx264', audio_codec='aac')
|