File size: 3,526 Bytes
3bb8cb9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>MP4 to WebM Converter</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 600px;
            margin: 20px auto;
            padding: 20px;
        }
        .container {
            border: 2px dashed #ccc;
            padding: 20px;
            text-align: center;
        }
        #preview {
            max-width: 100%;
            margin-top: 20px;
        }
        .hidden {
            display: none;
        }
        button {
            padding: 10px 20px;
            background: #4CAF50;
            color: white;
            border: none;
            cursor: pointer;
            margin: 10px;
        }
        button:disabled {
            background: #cccccc;
        }
    </style>
</head>
<body>
    <div class="container">
        <h2>MP4 to WebM Converter</h2>
        <input type="file" id="fileInput" accept="video/mp4">
        <button id="convertBtn" disabled>Convert to WebM</button>
        <a id="downloadLink" class="hidden"><button>Download WebM</button></a>
        <video id="preview" controls></video>
    </div>

    <script>
        const fileInput = document.getElementById('fileInput');
        const convertBtn = document.getElementById('convertBtn');
        const downloadLink = document.getElementById('downloadLink');
        const preview = document.getElementById('preview');

        let mediaRecorder;
        let recordedChunks = [];

        fileInput.addEventListener('change', async (e) => {
            const file = e.target.files[0];
            if (!file) return;

            const url = URL.createObjectURL(file);
            preview.src = url;
            convertBtn.disabled = false;
        });

        convertBtn.addEventListener('click', async () => {
            if (!preview.src) return;

            try {
                convertBtn.disabled = true;
                convertBtn.textContent = 'Converting...';
                
                const stream = await preview.captureStream();
                recordedChunks = [];
                
                mediaRecorder = new MediaRecorder(stream, {
                    mimeType: 'video/webm; codecs=vp9'
                });

                mediaRecorder.ondataavailable = (e) => {
                    if (e.data.size > 0) recordedChunks.push(e.data);
                };

                mediaRecorder.onstop = () => {
                    const blob = new Blob(recordedChunks, { type: 'video/webm' });
                    const url = URL.createObjectURL(blob);
                    downloadLink.href = url;
                    downloadLink.download = `converted_${Date.now()}.webm`;
                    downloadLink.classList.remove('hidden');
                    convertBtn.textContent = 'Convert to WebM';
                    convertBtn.disabled = false;
                };

                mediaRecorder.start();
                preview.play();

                // Остановить запись после завершения видео
                preview.addEventListener('ended', () => {
                    mediaRecorder.stop();
                });

            } catch (error) {
                console.error('Conversion error:', error);
                alert('Error during conversion: ' + error.message);
                convertBtn.textContent = 'Convert to WebM';
                convertBtn.disabled = false;
            }
        });
    </script>
</body>
</html>