hansaka1 commited on
Commit
bf1a231
·
verified ·
1 Parent(s): 6ccdea2

Update srv.py

Browse files
Files changed (1) hide show
  1. srv.py +19 -32
srv.py CHANGED
@@ -1,7 +1,6 @@
1
  from flask import Flask, request, jsonify, send_file, render_template
2
  from yt_dlp import YoutubeDL
3
  import os
4
- import subprocess
5
 
6
  app = Flask(__name__)
7
 
@@ -35,7 +34,7 @@ def get_info():
35
  return jsonify({'error': str(e)}), 500
36
 
37
  @app.route('/download', methods=['POST'])
38
- def download_video():
39
  data = request.json
40
  url = data.get('url')
41
 
@@ -43,50 +42,38 @@ def download_video():
43
  return jsonify({'error': 'URL is required'}), 400
44
 
45
  try:
46
- # Download video and audio streams
47
  ydl_opts = {
48
- 'format': 'bestvideo+bestaudio/best', # Download the best video and audio
49
- 'outtmpl': '%(title)s.%(ext)s', # Save file with title as filename
50
- 'cookiefile': 'www.youtube.com_cookies.txt', # Use cookies if needed
51
- 'noplaylist': True, # Avoid downloading playlists
 
 
 
 
 
52
  }
53
 
54
  with YoutubeDL(ydl_opts) as ydl:
 
55
  info = ydl.extract_info(url, download=True)
56
- video_file = ydl.prepare_filename(info)
57
 
58
- # Set the output file for merged video and audio
59
- output_file = video_file.rsplit('.', 1)[0] + '_merged.mp4'
60
-
61
- # Use FFmpeg to merge audio and video files
62
- video_file_path = video_file
63
- audio_file_path = video_file.rsplit('.', 1)[0] + '.m4a' # Assuming audio is in .m4a format
64
-
65
- # Merge audio and video using FFmpeg
66
- ffmpeg_command = [
67
- 'ffmpeg', '-i', video_file_path, '-i', audio_file_path,
68
- '-c:v', 'copy', '-c:a', 'aac', '-strict', 'experimental', output_file
69
- ]
70
- subprocess.run(ffmpeg_command, check=True)
71
-
72
- # Remove the original separate video and audio files to save space
73
- os.remove(video_file_path)
74
- os.remove(audio_file_path)
75
-
76
- # Send the merged video file to the client
77
  return send_file(
78
- output_file,
79
  as_attachment=True,
80
- download_name=os.path.basename(output_file)
81
  )
82
 
83
  except Exception as e:
84
  return jsonify({'error': str(e)}), 500
85
 
86
  finally:
87
- # Clean up the merged file after sending it
88
- if 'output_file' in locals() and os.path.exists(output_file):
89
- os.remove(output_file)
90
 
91
  if __name__ == '__main__':
92
  app.run(host='0.0.0.0', port=7860, debug=True)
 
1
  from flask import Flask, request, jsonify, send_file, render_template
2
  from yt_dlp import YoutubeDL
3
  import os
 
4
 
5
  app = Flask(__name__)
6
 
 
34
  return jsonify({'error': str(e)}), 500
35
 
36
  @app.route('/download', methods=['POST'])
37
+ def download_audio():
38
  data = request.json
39
  url = data.get('url')
40
 
 
42
  return jsonify({'error': 'URL is required'}), 400
43
 
44
  try:
45
+ # Set up yt-dlp options to download only audio
46
  ydl_opts = {
47
+ 'format': 'bestaudio/best', # Download the best available audio
48
+ 'outtmpl': '%(title)s.%(ext)s', # Output filename format
49
+ 'cookiefile': 'www.youtube.com_cookies.txt', # Use cookies file if needed
50
+ 'postprocessors': [{
51
+ 'key': 'FFmpegExtractAudio', # Extract audio with FFmpeg
52
+ 'preferredcodec': 'mp3', # Convert to MP3
53
+ 'preferredquality': '192', # Set audio quality
54
+ }],
55
+ 'noplaylist': True, # Avoid downloading entire playlists
56
  }
57
 
58
  with YoutubeDL(ydl_opts) as ydl:
59
+ # Extract video info and download the audio
60
  info = ydl.extract_info(url, download=True)
61
+ file_name = ydl.prepare_filename(info).rsplit(".", 1)[0] + ".mp3"
62
 
63
+ # Send the downloaded audio file to the client
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
  return send_file(
65
+ file_name,
66
  as_attachment=True,
67
+ download_name=os.path.basename(file_name)
68
  )
69
 
70
  except Exception as e:
71
  return jsonify({'error': str(e)}), 500
72
 
73
  finally:
74
+ # Clean up the file after sending
75
+ if 'file_name' in locals() and os.path.exists(file_name):
76
+ os.remove(file_name)
77
 
78
  if __name__ == '__main__':
79
  app.run(host='0.0.0.0', port=7860, debug=True)