Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const express = require('express')
- const router = express.Router()
- const fs = require('fs')
- const ytdl = require('ytdl-core')
- const youtube = require('simple-youtube-api')
- router.post('/downloadByUrl', async (req, res) => {
- let url = req.body.url
- let video = await PrepareVideo(url)
- res.header('Content-Disposition', 'attachment; filename=' + video.title.replace('|', '').toString('ascii') + '.mp4')
- return video.stream.pipe(res)
- })
- router.post('/downloadByName', async (req, res) => {
- let videoName = req.body.videoName
- let youtubeClient = new youtube('API KEY')
- await youtubeClient.searchVideos(videoName, 1)
- .then(async (results) => {
- let video = await PrepareVideo('https://youtu.be/' + results[0].id)
- res.header('Content-Disposition', 'attachment; filename=' + video.title.replace('|', '').toString('ascii') + '.mp4')
- return video.stream.pipe(res)
- })
- .catch(err => {
- console.log(err)
- return res.redirect('/')
- })
- })
- async function PrepareVideo(URL) {
- let videoReadableStream = ytdl(URL)
- let info = await ytdl.getInfo(URL)
- return { stream: videoReadableStream, title: info.title }
- }
- module.exports = router
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement