-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmp3tomp4.py
More file actions
112 lines (91 loc) · 4 KB
/
mp3tomp4.py
File metadata and controls
112 lines (91 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# ffmpeg -loop 1 -i image.png -i audio.mp3 -vf "pad=ceil(iw/2)*2:ceil(ih/2)*2" -c:a copy -c:v libx264 -shortest video.mp4
# ffmpeg -i video.mp4 -vcodec libx265 -crf 28 output_compressed.mp4
# Loading all the packages required
# from youtubesearchpython import VideosSearch
# videosSearch = VideosSearch('NoCopyrightSounds 30 seconds', limit = 2)
# print(videosSearch.result()['result'][0]['link'])
# replace 'shorts/' to 'watch?v=' in 'https://www.youtube.com/shorts/vRZOYPKebO0' if exists
from mutagen.mp3 import MP3
from PIL import Image
from pathlib import Path
from moviepy import editor
'''
Creating class MP3ToMP4 which contains methods to convert
an audio to a video using a list of images.
'''
class MP3ToMP4:
def __init__(self, folder_path, audio_path, video_path_name):
"""
:param folder_path: contains the path of the root folder.
:param audio_path: contains the path of the audio (mp3 file).
:param video_path_name: contains the path where the created
video will be saved along with the
name of the created video.
"""
self.folder_path = folder_path
self.audio_path = audio_path
self.video_path_name = video_path_name
# Calling the create_video() method.
self.create_video()
def get_length(self):
"""
This method reads an MP3 file and calculates its length
in seconds.
:return: length of the MP3 file
"""
song = MP3(self.audio_path)
return int(song.info.length)
def get_images(self):
"""
This method reads the filenames of the images present
in the folder_path of type '.png' and stores it in the
'images' list.
Then it opens the images, resizes them and appends them
to another list, 'image_list'
:return: list of opened images
"""
path_images = Path(self.folder_path)
images = list(path_images.glob('*.png'))
image_list = list()
for image_name in images:
image = Image.open(image_name).resize((800, 800), Image.ANTIALIAS)
image_list.append(image)
return image_list
def create_video(self):
"""
This method calls the get_length() and get_images()
methods internally. It then calculates the duration
of each frame. After that, it saves all the opened images
as a gif using the save() method. Finally it calls the
combine_method()
:return: None
"""
length_audio = self.get_length()
image_list = self.get_images()
print(f"length_audio: {length_audio} image_list: {image_list}")
duration = int(length_audio / len(image_list)) * 1000
image_list[0].save(self.folder_path + "temp.gif",
save_all=True,
append_images=image_list[1:],
duration=duration)
# Calling the combine_audio() method.
self.combine_audio()
def combine_audio(self):
"""
This method attaches the audio to the gif file created.
It opens the gif file and mp3 file and then uses
set_audio() method to attach the audio. Finally, it
saves the video to the specified video_path_name
:return: None
"""
video = editor.VideoFileClip(self.folder_path + "temp.gif")
audio = editor.AudioFileClip(self.audio_path)
final_video = video.set_audio(audio)
final_video.write_videofile(self.video_path_name, fps=60)
if __name__ == '__main__':
# Taking the input for the paths of the variables mentioned below.
folder_path = "/home/raz0229/Documents/python-instagram-command-bot/Photos"
audio_path = "/home/raz0229/Documents/python-instagram-command-bot/audio.mp3"
video_path_name = "/home/raz0229/Documents/python-instagram-command-bot/video.mp4"
# Invoking the parameterized constructor of the MP3ToMP4 class.
MP3ToMP4(folder_path, audio_path, video_path_name)