Skip to content

Instantly share code, notes, and snippets.

@rodolfoap
Last active March 20, 2024 12:57
Show Gist options
  • Save rodolfoap/cf198d9ea5d793b71ee5c021712e06d1 to your computer and use it in GitHub Desktop.
Save rodolfoap/cf198d9ea5d793b71ee5c021712e06d1 to your computer and use it in GitHub Desktop.
Video server
<!DOCTYPE html><html><body>
<video id="videoElement" width="320" height="240" autoplay controls></video>
<script src="/socket.io/socket.io.js"></script>
<script>
const socket = io();
const videoElement = document.getElementById('videoElement');
const mediaSource = new MediaSource();
videoElement.src = URL.createObjectURL(mediaSource);
socket.on('connect', () => {socket.emit('offer', 'start');});
mediaSource.addEventListener("sourceopen", (event) => {
const sourceBuffer = mediaSource.addSourceBuffer('video/webm; codecs=vp8');
socket.on('data', (data) => {
// console.log(sourceBuffer.buffered);
console.log("data: "+data.byteLength+" bytes");
sourceBuffer.appendBuffer(data);
});
});
</script>
</body></html>
const express = require('express');
const app = express();
const http = require('http');
const server = http.createServer(app);
const socketIo = require('socket.io');
const io = socketIo(server);
const ffmpegPath = require('@ffmpeg-installer/ffmpeg').path;
const ffmpeg = require('fluent-ffmpeg');
ffmpeg.setFfmpegPath(ffmpegPath);
app.get('/', (req, res) => {res.sendFile(__dirname + '/index.html');});
io.on('connection', (socket) => {
console.log('connection');
socket.on('offer', (offer) => {
console.log(`offer: ${offer}`);
ffmpeg()
.input('video.mp4')
.inputFormat('mp4')
.outputOptions(['-vf', 'scale=320:240', '-an', '-pix_fmt', 'yuv420p', '-vcodec', 'libvpx', '-preset', 'slow', '-crf', '18', '-strict', 'experimental'])
.outputFormat('webm')
.pipe()
.on('data', function(data) {
console.log('emit: ' + data.length + ' bytes');
socket.emit('data', data );
});
})
});
server.listen(3000, () => {console.log('Server running on http://localhost:3000');});
This file has been truncated, but you can view the full file.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment