Last active 1610104246

A script I made a long time ago, generates a video for a music file using cover art if available and including track information

Alyssa Smith revised this gist 1610140245. Go to revision

1 file changed, 37 insertions

videolp.py(file created)

@@ -0,0 +1,37 @@
1 + #!/usr/bin/env python3
2 + # A script to generate videos for MP3s
3 + from moviepy.editor import *
4 + import numpy as np
5 + from PIL import Image
6 + from glob import glob
7 + from mutagen.mp3 import MP3
8 + from io import BytesIO
9 +
10 + #points
11 + ART_TOP_LEFT = (620,45)
12 + ART_SIZE = (585,585)
13 + TEXT_TOP_LEFT = (85,60)
14 + TEXT_SIZE = (510,565)
15 +
16 + for fn in sorted(glob("*.mp3")):
17 + data = MP3(fn)
18 + audio = AudioFileClip(fn)
19 + text = TextClip("You are listening to\n\n{TIT2.text[0]}\n\nBy {TPE1.text[0]}\n\nFrom {TALB.text[0]}".format(**data.tags).encode('utf8'),
20 + size=TEXT_SIZE,
21 + color="white",
22 + font="Corbel",
23 + kerning=5,
24 + fontsize=50,
25 + method="caption",
26 + align="West").set_position(TEXT_TOP_LEFT).set_duration(audio.duration).set_audio(audio)
27 + cover_art = [k for k in data.tags.keys() if k[:4] == "APIC"]
28 + if cover_art:
29 + if len(cover_art) != 1:
30 + print("Using {} for cover art".format(cover_art[0]))
31 + img_input = np.asarray( Image.open( BytesIO(data.tags[cover_art[0]].data) ), dtype="int32" )
32 + else:
33 + print("Using cover.jpg for cover art")
34 + img_input = "cover.jpg"
35 + cover = ImageClip(img_input).set_position(ART_TOP_LEFT).set_duration(audio.duration).resize(ART_SIZE)
36 + cvc = CompositeVideoClip([text, cover], size=(1280,720)).set_fps(60)
37 + cvc.write_videofile(fn.replace(".mp3", ".mp4"))
Newer Older