why the client side catch the socket.emit after delay from the server side?

22 views Asked by At

The yt_dlp library in Python is an exceptional tool for downloading and converting songs from YouTube into local mp3 files. I want to display the name of the song when the download is finished. I sent socket.io from the Hook method using yt_dlp, but there was a delay in the emit catch on the client side.

server-side

def download_video(urls):
    ydl_opts = `your text`{
        'format': 'bestaudio/best',
        'ffmpeg_location': 'C:/ffmpeg/bin',
        'outtmpl': './downloads/%(title)s.%(ext)s',  # Choose the best quality audio
        'postprocessors': [{ 
            'key': 'FFmpegExtractAudio',
            'preferredcodec': 'mp3',
            'preferredquality': '192',  # You can modify the quality as needed.
        }],
        'progress_hooks': [hook],  # Add the hook function here
    }
    with ydlp.YoutubeDL(ydl_opts) as ydl:
        for url in urls:
            ydl.download([url])
def hook(d):
    with app.app_context():
      downloaded_bytes = d.get('downloaded_bytes', 0)
      total_bytes = d.get('total_bytes', 0)
      progress_percentage = (downloaded_bytes / total_bytes) * 100
      if d['status'] == 'downloading':
        print(f"Downloading: {d['filename']}, {d['downloaded_bytes']}/{d['total_bytes']}")


      if d['status'] == 'finished':
           print(f"\nDone downloading {d['filename']}! Now converting...\n")
          try:    
             
             socketio.emit('update_progress', {'songName': 
             d['filename'],'type': 2},    room=user_id)
             sleep(0.1)
             
          except Exception as e:
             print(f"Error emitting: {e}")

client side

           socket.on('update_progress', function (data) {

                 var songList = document.getElementById('songList');

                 // Create a new list item and set its text to the song name from the emit data
                 var listItem = document.createElement('li');
                 listItem.textContent = data.songName;

                 // Append the list item to the song list
                 songList.appendChild(listItem);
});

Do you have a solution to add the name to the list after the file downloading?

I added try and catch to understand if I have a problem with the connection socket.io. I added a delay for sending a buffer between the server side and to client-side

0

There are 0 answers