vikarshana commited on
Commit
3fee868
·
verified ·
1 Parent(s): b605d72

Update srv.py

Browse files
Files changed (1) hide show
  1. srv.py +34 -28
srv.py CHANGED
@@ -34,62 +34,68 @@ def get_info():
34
  return jsonify({'error': str(e)}), 500
35
 
36
  @app.route('/download', methods=['POST'])
37
- def download_media():
38
  data = request.json
39
  url = data.get('url')
40
- format_type = data.get('format', 'mp4') # Default format: MP4
41
- quality = str(data.get('quality', 'best')) # Default: best quality
42
 
43
  if not url:
44
  return jsonify({'error': 'URL is required'}), 400
45
 
46
  try:
47
- if format_type == 'mp4':
48
- quality_map = {
49
- '144': 'bv*[height=144][ext=mp4]+ba[ext=m4a]/b[height=144][ext=mp4]',
50
- '360': 'bv*[height=360][ext=mp4]+ba[ext=m4a]/b[height=360][ext=mp4]',
51
- '480': 'bv*[height=480][ext=mp4]+ba[ext=m4a]/b[height=480][ext=mp4]',
52
- '720': 'bv*[height=720][ext=mp4]+ba[ext=m4a]/b[height=720][ext=mp4]',
53
- '1080': 'bv*[height=1080][ext=mp4]+ba[ext=m4a]/b[height=1080][ext=mp4]',
54
- 'best': 'bv*[ext=mp4]+ba[ext=m4a]/b[ext=mp4]' # Best available
55
- }
56
-
57
- ydl_opts = {
58
- 'format': quality_map.get(quality, quality_map['best']),
59
- 'merge_output_format': 'mp4',
60
- 'outtmpl': '%(title)s.%(ext)s',
61
- 'cookiefile': 'www.youtube.com_cookies.txt'
62
- }
63
- else: # Default: MP3
64
  ydl_opts = {
65
- 'format': 'bestaudio',
66
  'outtmpl': '%(title)s.%(ext)s',
67
  'cookiefile': 'www.youtube.com_cookies.txt',
68
  'postprocessors': [{
69
  'key': 'FFmpegExtractAudio',
70
  'preferredcodec': 'mp3',
71
- 'preferredquality': '64',
72
  }],
73
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
  with YoutubeDL(ydl_opts) as ydl:
75
  info = ydl.extract_info(url, download=True)
76
- file_ext = "mp4" if format_type == "mp4" else "mp3"
77
- file_name = ydl.prepare_filename(info).rsplit(".", 1)[0] + f".{file_ext}"
 
 
 
78
 
79
- # Send file to client
80
  return send_file(
81
  file_name,
82
  as_attachment=True,
83
- download_name=os.path.basename(file_name)
 
84
  )
85
 
86
  except Exception as e:
87
  return jsonify({'error': str(e)}), 500
88
 
89
  finally:
90
- # Delete file after sending
91
  if 'file_name' in locals() and os.path.exists(file_name):
92
  os.remove(file_name)
93
 
94
  if __name__ == '__main__':
95
- app.run(host='0.0.0.0', port=7860, debug=True)
 
34
  return jsonify({'error': str(e)}), 500
35
 
36
  @app.route('/download', methods=['POST'])
37
+ def download():
38
  data = request.json
39
  url = data.get('url')
40
+ format_type = data.get('format', 'mp3')
41
+ quality = data.get('quality', 'best')
42
 
43
  if not url:
44
  return jsonify({'error': 'URL is required'}), 400
45
 
46
  try:
47
+ if format_type == 'mp3':
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
  ydl_opts = {
49
+ 'format': 'bestaudio/best',
50
  'outtmpl': '%(title)s.%(ext)s',
51
  'cookiefile': 'www.youtube.com_cookies.txt',
52
  'postprocessors': [{
53
  'key': 'FFmpegExtractAudio',
54
  'preferredcodec': 'mp3',
55
+ 'preferredquality': '192',
56
  }],
57
  }
58
+ else:
59
+ quality_map = {
60
+ '144': 'bv*[height=144][ext=mp4]+ba[ext=m4a]/b[height=144][ext=mp4]',
61
+ '240': 'bv*[height=240][ext=mp4]+ba[ext=m4a]/b[height=240][ext=mp4]',
62
+ '360': 'bv*[height=360][ext=mp4]+ba[ext=m4a]/b[height=360][ext=mp4]',
63
+ '480': 'bv*[height=480][ext=mp4]+ba[ext=m4a]/b[height=480][ext=mp4]',
64
+ '720': 'bv*[height=720][ext=mp4]+ba[ext=m4a]/b[height=720][ext=mp4]',
65
+ '1080': 'bv*[height=1080][ext=mp4]+ba[ext=m4a]/b[height=1080][ext=mp4]',
66
+ 'best': 'bv*[ext=mp4]+ba[ext=m4a]/b[ext=mp4]'
67
+ }
68
+
69
+ selected_quality = quality_map.get(quality, quality_map['best'])
70
+
71
+ ydl_opts = {
72
+ 'format': selected_quality,
73
+ 'outtmpl': '%(title)s.%(ext)s',
74
+ 'merge_output_format': 'mp4',
75
+ 'cookiefile': 'www.youtube.com_cookies.txt',
76
+ }
77
+
78
  with YoutubeDL(ydl_opts) as ydl:
79
  info = ydl.extract_info(url, download=True)
80
+
81
+ if format_type == 'mp3':
82
+ file_name = ydl.prepare_filename(info).rsplit(".", 1)[0] + ".mp3"
83
+ else:
84
+ file_name = ydl.prepare_filename(info)
85
 
 
86
  return send_file(
87
  file_name,
88
  as_attachment=True,
89
+ download_name=os.path.basename(file_name),
90
+ mimetype='video/mp4' if format_type == 'mp4' else 'audio/mp3'
91
  )
92
 
93
  except Exception as e:
94
  return jsonify({'error': str(e)}), 500
95
 
96
  finally:
 
97
  if 'file_name' in locals() and os.path.exists(file_name):
98
  os.remove(file_name)
99
 
100
  if __name__ == '__main__':
101
+ app.run(host='0.0.0.0', port=7860, debug=True)