I am working on a youtube audio player just like Rythm/Groovy from discord.I have few other features like downloading the audio file.[not working]]

1

I am using flask and javascript for this. I am able to download the audio file as requested by the user using an ajax call to the server and can convert it to mp3 from webm but on sending it back to the web app from my computer the audio file gets corrupted. this is my code

$(document).ready(function() {

$('#download').click(function(event) {

    $.ajax({
        data : {
    name:"1",
            url : player.getVideoUrl(),
    title: document.getElementById("songTitle").innerHTML,
        },
        type : 'POST',
        url : '/play',
    })
.done(function(data){
  console.log(data);
  const a = document.createElement("a");
  a.style.display = "none";
  document.body.appendChild(a);
  var type="audio/mpeg"
  // Set the HREF to a Blob representation of the data to be downloaded
  a.href = window.URL.createObjectURL(
    new Blob([data], { type })
  );

  // Use download attribute to set set desired file name
  a.setAttribute("download",document.getElementById("songTitle").innerHTML );

  // Trigger the download by simulating click
  a.click();

  // Cleanup
  window.URL.revokeObjectURL(a.href);
  document.body.removeChild(a);
});


    event.preventDefault();

});

});

This is my code on my application.py file that sends back the audio file

@app.route('/play',methods=["GET","POST"])
def e():
if request.method == "POST":
    if request.form['name']=="1":
        attempted_url = request.form["url"]
        if attempted_url != "":
            result_id = get_media(attempted_url)
            session["url"] = attempted_url
            session["id"] = result_id
            filename = request.form["title"]
            session["filename"] = filename
            return redirect(url_for("return_file"))
    else:
        #this is a different set of code which returns data for a searched name
        data = yt_cal(request.form['name'])
        return jsonify(data = data)
elif len(temp)==1 and k[0] == 0:
    k[0] = 1
    for data in finaldata[0]:
        if data['id'] == int(temp[0]):
            temp2 = data
            return render_template("play.html",data = temp2)


@app.route("/return-file/")
def return_file():

filename = session.get("filename")
filename_formatted = filename + ".mp3"
downloads/{}.mp3".format(session.get("id"))
location = "media/Audio downloads/{}.mp3".format(session.get("id"))

return send_file(
    location, attachment_filename=filename_formatted, as_attachment=True
)

The file gets downloaded on the webpage but when I try to open it again on my computer it throws the 0xc00d36c4 error. I have checked if the downloaded mp3 file works before sending it to the web app and it does, somewhere in between it is getting corrupted. also, I tried to add contentType: "audio/mpeg" to the ajax request but then it returns 400 on the ajax call.

the image on the left is the one which is getting downloaded after going through the ajax call which is not working and the one on the right is the one which is getting downloaded using yt_dl library which works

javascript
python
ytdl
asked on Stack Overflow Mar 18, 2021 by Suchindra Walavalkar • edited Mar 18, 2021 by Suchindra Walavalkar

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0