Skip to content

Instantly share code, notes, and snippets.

@neybapps
Last active August 18, 2024 18:26
Show Gist options
  • Save neybapps/c086017125459e1787f0de6bac0d4793 to your computer and use it in GitHub Desktop.
Save neybapps/c086017125459e1787f0de6bac0d4793 to your computer and use it in GitHub Desktop.
Video processing using Node.js/Express.js and ffmpeg with Typescript
// prerequisite: https://gist.github.com/neybapps/1e506cbc0eb536812c63184abe3b96e4
// prerequisite: sudo apt-get update && sudo apt-get install -y ffmpeg
npm install fluent-ffmpeg
npm install --save-dev @types/fluent-ffmpeg
// under index.ts add:
import express from 'express';
import ffmpeg from 'fluent-ffmpeg';
const app = express();
app.use(express.json());
app.post('/process-video', (req, res) => {
// Get the path of the input video file from the request body
const inputFilePath = req.body.inputFilePath;
const outputFilePath = req.body.outputFilePath;
// Check if the input file path is defined
if (!inputFilePath || !outputFilePath) {
return res.status(400).send('Bad Request: Missing file path');
}
// Create the ffmpeg command
ffmpeg(inputFilePath)
.outputOptions('-vf', 'scale=-1:360') // 360p
.on('end', function() {
console.log('Processing finished successfully');
res.status(200).send('Processing finished successfully');
})
.on('error', function(err: any) {
console.log('An error occurred: ' + err.message);
res.status(500).send('An error occurred: ' + err.message);
})
.save(outputFilePath);
});
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment