File size: 4,549 Bytes
50695f9
 
 
 
fc72765
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50695f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fc72765
 
 
50695f9
 
 
fc72765
50695f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
from moviepy.editor import *
from pydub import AudioSegment

# class VideoConverter:
#     def __init__(self, input_file):
#         self.input_file = input_file
#         self.video = None
#         self.audio = None

#         if not os.path.exists(self.input_file):
#             raise FileNotFoundError(f"File not found: {self.input_file}")

#         self.load_video()

#     def load_video(self):
#         try:
#             self.video = VideoFileClip(self.input_file)
#             self.audio = self.video.audio
#         except Exception as e:
#             raise Exception(f"Error loading video: {e}")

#     def convert_video(self, output_file, format):
#         if format not in ['webm', 'wmv', 'mkv', 'mp4', 'avi', 'mpeg', 'vob', 'flv']:
#             raise ValueError(f"Unsupported format: {format}")

#         try:
#             self.video.write_videofile(output_file, codec=format.lower())
#             print(f"Video converted to {format} format successfully!")
#             return output_file
#         except Exception as e:
#             raise Exception(f"Error converting video: {e}")


#     def convert_audio(self, output_file, format):
#         if format not in ['mp3', 'wav', 'ogg', 'flac', 'aac']:
#             raise ValueError(f"Unsupported format: {format}")

#         try:
#             audio_segment = AudioSegment.from_file(self.input_file, self.audio.codec)
#             audio_segment.export(output_file, format=format.lower())
#             print(f"Audio converted to {format} format successfully!")
#             return output_file
#         except Exception as e:
#             raise Exception(f"Error converting audio: {e}")

# def convert_video(input_file, format):
#     try:
#         converter = VideoConverter(input_file)
#         if format in ['webm', 'wmv', 'mkv', 'mp4', 'avi', 'mpeg', 'vob', 'flv']:
#             return None, converter.convert_video(f"output.{format}", format), "Converted video successfully!"
#         elif format in ['mp3', 'wav', 'ogg', 'flac', 'aac']:
#             return converter.convert_audio(f"output.{format}", format), None, "Converted audio successfully!"
#         else:
#             return None, None, "Unsupported format!"
#     except Exception as e:
#         return None, None, str(e)

class VideoConverter:
    def __init__(self, input_file):
        self.input_file = input_file
        self.video = None
        self.audio = None

        if not os.path.exists(self.input_file):
            raise FileNotFoundError(f"File not found: {self.input_file}")

        self.load_video()

    def load_video(self):
        try:
            self.video = VideoFileClip(self.input_file)
            self.audio = self.video.audio
        except Exception as e:
            raise Exception(f"Error loading video: {e}")

    def convert_video(self, output_file, format):
        video_codecs = {'webm': 'libvpx', 'wmv': 'wmv2', 'mkv': 'libx264', 'mp4': 'libx264', 'avi': 'libxvid', 'mpeg': 'mpeg2video', 'vob': 'mpeg2video', 'flv': 'flv'}

        if format not in video_codecs:
            raise ValueError(f"Unsupported format: {format}")

        try:
            self.video.write_videofile(output_file, codec=video_codecs[format.lower()])
            print(f"Video converted to {format} format successfully!")
            return output_file
        except Exception as e:
            raise Exception(f"Error converting video: {e}")

    def convert_audio(self, output_file, format):
        if format not in ['mp3', 'wav', 'ogg', 'flac', 'aac']:
            raise ValueError(f"Unsupported format: {format}")

        try:
            audio_segment = AudioSegment.from_file(self.input_file, self.audio.codec)
            audio_segment.export(output_file, format=format.lower())
            print(f"Audio converted to {format} format successfully!")
            return output_file
        except Exception as e:
            raise Exception(f"Error converting audio: {e}")

def convert_video(input_file, format):
    try:
        converter = VideoConverter(input_file)
        if format in ['webm', 'wmv', 'mkv', 'mp4', 'avi', 'mpeg', 'vob', 'flv']:
            return None, converter.convert_video(f"output.{format}", format), "Converted video successfully!"
        elif format in ['mp3', 'wav', 'ogg', 'flac', 'aac']:
            return converter.convert_audio(f"output.{format}", format), None, "Converted audio successfully!"
        else:
            return None, None, "Unsupported format!"
    except Exception as e:
        return None, None, str(e)