hansaka1 commited on
Commit
5557d6a
·
verified ·
1 Parent(s): 4fd3c44

Update srv.py

Browse files
Files changed (1) hide show
  1. srv.py +9 -13
srv.py CHANGED
@@ -34,7 +34,7 @@ def get_info():
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,22 +42,18 @@ def download_audio():
42
  return jsonify({'error': 'URL is required'}), 400
43
 
44
  try:
 
45
  ydl_opts = {
46
- 'format': '251/bestaudio', # Format untuk audio terbaik
47
- 'outtmpl': '%(title)s.%(ext)s',
48
- 'cookiefile': 'www.youtube.com_cookies.txt',
49
- 'postprocessors': [{
50
- 'key': 'FFmpegExtractAudio',
51
- 'preferredcodec': 'mp3',
52
- 'preferredquality': '192',
53
- }],
54
  }
55
 
56
  with YoutubeDL(ydl_opts) as ydl:
57
  info = ydl.extract_info(url, download=True)
58
- file_name = ydl.prepare_filename(info).rsplit(".", 1)[0] + ".mp3"
59
 
60
- # Kirim file ke client
61
  return send_file(
62
  file_name,
63
  as_attachment=True,
@@ -68,9 +64,9 @@ def download_audio():
68
  return jsonify({'error': str(e)}), 500
69
 
70
  finally:
71
- # Bersihkan file setelah dikirim
72
  if 'file_name' in locals() and os.path.exists(file_name):
73
  os.remove(file_name)
74
 
75
  if __name__ == '__main__':
76
- 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_video():
38
  data = request.json
39
  url = data.get('url')
40
 
 
42
  return jsonify({'error': 'URL is required'}), 400
43
 
44
  try:
45
+ # Update the options to download video only
46
  ydl_opts = {
47
+ 'format': 'bestvideo/best', # Download the best available video
48
+ 'outtmpl': '%(title)s.%(ext)s', # Save the file with title as filename
49
+ 'cookiefile': 'www.youtube.com_cookies.txt', # Use cookies (if needed)
 
 
 
 
 
50
  }
51
 
52
  with YoutubeDL(ydl_opts) as ydl:
53
  info = ydl.extract_info(url, download=True)
54
+ file_name = ydl.prepare_filename(info)
55
 
56
+ # Send the video file to the client
57
  return send_file(
58
  file_name,
59
  as_attachment=True,
 
64
  return jsonify({'error': str(e)}), 500
65
 
66
  finally:
67
+ # Clean up the file after sending it
68
  if 'file_name' in locals() and os.path.exists(file_name):
69
  os.remove(file_name)
70
 
71
  if __name__ == '__main__':
72
+ app.run(host='0.0.0.0', port=7860, debug=True)