0

I am looking for a way to send the url to the nodejs server and respond the user with the mp3 file download.

I searched some examples, and read about requests and responses, but I am not sure what the problem really is.

This is the Javascript for the HTML:

    var downloadBtn = document.querySelector('.download_button');
    var URLinput = document.querySelector('#myUrl');

    downloadBtn.addEventListener('click', () => {
        console.log(`URL: ${URLinput.value}`);
        sendURL(URLinput.value);
    });

    function sendURL(URL) {
        window.location.href = `http://localhost:4000/download?URL=${URL}`;
    }

This is the Javascript for the Nodejs server:

const express = require('express');
const cors = require('cors');
const ytdl = require('ytdl-core');
const app = express();
const ffmpeg = require('fluent-ffmpeg')
app.use(cors());

app.listen(4000, () => {
    console.log('Server Works !!! At port 4000');
});

app.get('/download', (req,res) => {
var URL = req.query.URL;

res.header('Content-Disposition', 'attachment; filename="file.mp3"');
let stream = ytdl(URL, { 
  quality: 'highestaudio',
}); //HERE THE STREAM FILE IS SELECTED TO BE CONVERTED TO MP3

ffmpeg(stream)
  .audioBitrate(128)
  .pipe(res); // HERE IS CONVERTED AND WHERE I WANT IT TO SEND IT AS A DOWNLOAD TO THE USER.
});

I expected it to stream download the file but instead it gets me to the nodejs server page to /download/url_to_vid

Thriskel
  • 351
  • 3
  • 13
  • 1
    Can you explain little more what exact issue? Do you have mp3 in specified url location. Also conform the mp3 filename you mentioned file.mp3, Specify the link where you took these examples – Rasa Mohamed Feb 18 '19 at 05:15
  • @RasaMohamed I added comments to the parts of the code that answer your question, aditionally, the issue is that it is not sending back the converted mp3 file. If I were to modify the code piping the ytdl instead of the ffmpeg it would work for the mp4 file, but I don't want that, I want the mp3 output of the ffmpeg convertion. – Thriskel Feb 19 '19 at 13:11
  • I think [this](https://stackoverflow.com/questions/18168432/node-js-how-to-pipe-youtube-to-mp4-to-mp3/18168691) will help you – Rasa Mohamed Feb 19 '19 at 14:29
  • @RasaMohamed I used it as one of the examples, but sends the user to the "http://localhost:4000/download?URL=videourl" and the nodejs server gives an error "too many redirects" – Thriskel Feb 19 '19 at 16:22

0 Answers0