I am using Youtube-dl in NodeJS using child process-spawn and piping the output video as download to the browser. The code works as expected but sometimes an empty file is sent. Now if I restart the dyno, the app works perfectly fine? The app is hosted on Heroku free tier. No error is thrown.
/*Downloading ,Converting mp4 youtube video using video_id */
const ytdl = spawn('youtube-dl', [
'-o',//output
'-',//stdout
'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best',//best mp4 extension , else best
'--recode-video',//recode video
'mp4',//to mp4 if not mp4
'-a',//input stream
'-'//stdin
])
.on('error',(err)=>next(err))
.on('exit',(code)=>console.log(`Ytdl exited with code ${code}`));
/* Setting output pipe first so that we dont lose any bits */
ytdl.stdout.pipe(res).on('error',(err)=>next(err));
/*Catching error on stdin */
ytdl.stdin.on('error',(err)=>next(err));
/* Writing video url to stdin for youtube-dl */
ytdl.stdin.write(`http://www.youtube.com/watch?v=${vid}`)
/*Closing the input stream*/
ytdl.stdin.end();
The video is downloaded correctly about 70% time but other times empty file is downloaded for same video.
I wonder if it has to do something with killing child process or closing streams or Heroku dynos sleep?
Log during Empty Download:
2020-01-17T17:36:36.704915+00:00 heroku[router]: at=info method=GET path="/youtube/download/video/KBtk5FUeJbk/Sub%20Urban%20-%20Cradles%20%5BOFFICIAL%20MUSIC%20VIDEO%5D" host=youtube-plus.herokuapp.com request_id=5ec18598-f702-421d-aec9-52d4e8a8fc33 fwd="150.242.75.17" dyno=web.1 connect=0ms service=1953ms status=200 bytes=234 protocol=https
2020-01-17T17:36:36.704195+00:00 app[web.1]: GET /youtube/download/video/KBtk5FUeJbk/Sub%20Urban%20-%20Cradles%20%5BOFFICIAL%20MUSIC%20VIDEO%5D 200 1952.343 ms - -
2020-01-17T17:36:36.704561+00:00 app[web.1]: Ytdl exited with code 1
Log for same file on correct download :
2020-01-17T19:09:32.400885+00:00 heroku[router]: at=info method=GET path="/youtube/download/video/KBtk5FUeJbk/Sub%20Urban%20-%20Cradles%20%5BOFFICIAL%20MUSIC%20VIDEO%5D" host=youtube-plus.herokuapp.com request_id=f72a8672-6031-4b7d-9f93-e1eaf50be0d9 fwd="150.242.75.17" dyno=web.1 connect=0ms service=4922ms status=200 bytes=15774017 protocol=https
2020-01-17T19:09:32.400201+00:00 app[web.1]: GET /youtube/download/video/KBtk5FUeJbk/Sub%20Urban%20-%20Cradles%20%5BOFFICIAL%20MUSIC%20VIDEO%5D 200 3609.879 ms - -
2020-01-17T19:09:32.400722+00:00 app[web.1]: Ytdl exited with code 1
Okay so I figured out the solution. The problem is youtube frequently updates its website, so we need to have the latest youtube-dl installed in the Dynos. But to reinstall buildpack on heroku, we need to redeploy the app.
git commit --allow-empty -m"Updating BuildPacks And Redeploy To Heroku"
git push heroku master
The latest versions of buildpack are installed and app works correctly.