Skip to content

Instantly share code, notes, and snippets.

@djavier
Created December 1, 2020 11:44
Show Gist options
  • Save djavier/88a7fa2dcf0a82faa0cdde5566f11b02 to your computer and use it in GitHub Desktop.
Save djavier/88a7fa2dcf0a82faa0cdde5566f11b02 to your computer and use it in GitHub Desktop.
Handy script to convert mp3 to mp4 and place cover as image video.
from PIL import Image
from mutagen.id3 import ID3
from mutagen.mp3 import MP3
from io import BytesIO
import sys
import os
import glob
import ffmpeg
dir_path = os.path.dirname(os.path.realpath(__file__))
mp3Files = []
for file in glob.glob("*.mp3"):
mp3Files.append(file)
song_path = os.path.join(dir_path, file)
song_name = os.path.splitext(file)[0]
img_path = song_name + '.jpg'
video_path = song_name + '.mp4'
track = MP3(song_path)
tags = track.tags
print(tags.pprint())
print("LENGTH=" + str(track.info.length))
pict = tags.get("APIC:").data
im = Image.open(BytesIO(pict))
im.save(img_path)
print('Picture size : ' + str(im.size))
print('Picture size : ' + str(im.size[0]))
width = im.size[0]
height = im.size[1]
if (im.size[0] % 2 != 0):
width = im.size[0] + 1
if (im.size[1] % 2 != 0):
height = im.size[1] + 1
in_image = ffmpeg.input(img_path).filter(
'scale', size=str(width) + ":" + str(height), force_original_aspect_ratio='increase')
in_audio = ffmpeg.input(song_path)
(
ffmpeg
.concat(in_image, in_audio, v=1, a=1)
.output(video_path)
.run(overwrite_output=True)
)
# joined = ffmpeg.concat(in_file, in_image).node
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment