Created
October 5, 2023 13:59
-
-
Save betiol/ee6ab9fc2cf0ca5bc41aa0b987c52821 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
const ytdl = require('ytdl-core'); | |
const ffmpeg = require('fluent-ffmpeg'); | |
const fs = require('fs'); | |
const app = express(); | |
const port = 3000; | |
const startMinute = 3; | |
const startSecond = 51; | |
const endMinute = 4; | |
const endSecond = 51; | |
const startTime = startMinute * 60 + startSecond; | |
const endTime = endMinute * 60 + endSecond; | |
const duration = endTime - startTime; | |
app.use(express.json()); | |
app.post('/download-and-cut', async (req, res) => { | |
try { | |
const { videoUrl} = req.body; | |
if (!videoUrl || !startTime || !endTime) { | |
return res.status(400).json({ error: 'Parâmetros inválidos' }); | |
} | |
const videoFileName = `${Date.now()}.mp4`; | |
const videoStream = ytdl(videoUrl, { quality: 'highestvideo', filter: 'audioandvideo', }); | |
ffmpeg({ timeout: 120000 }) | |
.input(videoStream) | |
.setStartTime(startTime) | |
.setDuration(duration) | |
.size('1080x1920') | |
.videoFilters('scale=1080:1920,setsar=1:1') | |
.outputOptions('-c:v libx264') | |
.audioCodec('aac') | |
.outputOptions('-r 60') | |
.output(videoFileName) | |
.on('end', () => { | |
const outputPath = `./downloads/${videoFileName}`; | |
fs.renameSync(videoFileName, outputPath); | |
res.json({ message: 'cropped with success', filePath: outputPath }); | |
}) | |
.on('error', (err) => { | |
console.error('Error:', err); | |
res.status(500).json({ error: 'Error' }); | |
}) | |
.run(); | |
} catch (err) { | |
console.error('Unexpected error:', err); | |
res.status(500).json({ error: 'Unexpected error' }); | |
} | |
}); | |
app.listen(port, () => { | |
console.log(`Server running at port ${port}`); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment