vikarshana commited on
Commit
5964f9b
·
verified ·
1 Parent(s): 862b90d

Update srv.py

Browse files
Files changed (1) hide show
  1. srv.py +20 -16
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 traceback
5
 
6
  app = Flask(__name__)
7
 
@@ -44,31 +43,32 @@ def download():
44
  if not url:
45
  return jsonify({'error': 'URL is required'}), 400
46
 
47
- file_name = None # Initialize file_name to avoid reference issues
48
-
49
  try:
50
  if format_type == 'mp3':
51
  ydl_opts = {
52
  'format': 'bestaudio/best',
53
  'outtmpl': '%(title)s.%(ext)s',
54
  'cookiefile': 'www.youtube.com_cookies.txt',
 
 
 
55
  'postprocessors': [{
56
  'key': 'FFmpegExtractAudio',
57
  'preferredcodec': 'mp3',
58
- 'preferredquality': '192',
59
- 'nopostoverwrites': True, # Prevent overwriting existing files
60
  }],
61
  }
62
- else:
63
  quality_map = {
64
- '144': 'bv*[height=144][ext=mp4]+ba[ext=m4a]/b[height=144][ext=mp4]',
65
- '240': 'bv*[height=240][ext=mp4]+ba[ext=m4a]/b[height=240][ext=mp4]',
66
- '360': 'bv*[height=360][ext=mp4]+ba[ext=m4a]/b[height=360][ext=mp4]',
67
- '480': 'bv*[height=480][ext=mp4]+ba[ext=m4a]/b[height=480][ext=mp4]',
68
- '720': 'bv*[height=720][ext=mp4]+ba[ext=m4a]/b[height=720][ext=mp4]',
69
- '1080': 'bv*[height=1080][ext=mp4]+ba[ext=m4a]/b[height=1080][ext=mp4]',
70
- 'best': 'bv*[ext=mp4]+ba[ext=m4a]/b[ext=mp4]'
71
- }
72
 
73
  selected_quality = quality_map.get(quality, quality_map['best'])
74
 
@@ -77,6 +77,10 @@ def download():
77
  'outtmpl': '%(title)s.%(ext)s',
78
  'merge_output_format': 'mp4',
79
  'cookiefile': 'www.youtube.com_cookies.txt',
 
 
 
 
80
  }
81
 
82
  with YoutubeDL(ydl_opts) as ydl:
@@ -95,10 +99,10 @@ def download():
95
  )
96
 
97
  except Exception as e:
98
- return jsonify({'error': str(e), 'traceback': traceback.format_exc()}), 500
99
 
100
  finally:
101
- if file_name and os.path.exists(file_name):
102
  os.remove(file_name)
103
 
104
  if __name__ == '__main__':
 
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
 
 
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
+ 'headers': {
53
+ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'
54
+ },
55
  'postprocessors': [{
56
  'key': 'FFmpegExtractAudio',
57
  'preferredcodec': 'mp3',
58
+ 'preferredquality': '192',
59
+ 'nopostoverwrites': True,
60
  }],
61
  }
62
+ else:
63
  quality_map = {
64
+ '144': 'bv*[height=144][ext=mp4]+ba[ext=m4a]/b[height=144][ext=mp4]',
65
+ '240': 'bv*[height=240][ext=mp4]+ba[ext=m4a]/b[height=240][ext=mp4]',
66
+ '360': 'bv*[height=360][ext=mp4]+ba[ext=m4a]/b[height=360][ext=mp4]',
67
+ '480': 'bv*[height=480][ext=mp4]+ba[ext=m4a]/b[height=480][ext=mp4]',
68
+ '720': 'bv*[height=720][ext=mp4]+ba[ext=m4a]/b[height=720][ext=mp4]',
69
+ '1080': 'bv*[height=1080][ext=mp4]+ba[ext=m4a]/b[height=1080][ext=mp4]',
70
+ 'best': 'bv*[ext=mp4]+ba[ext=m4a]/b[ext=mp4]'
71
+ }
72
 
73
  selected_quality = quality_map.get(quality, quality_map['best'])
74
 
 
77
  'outtmpl': '%(title)s.%(ext)s',
78
  'merge_output_format': 'mp4',
79
  'cookiefile': 'www.youtube.com_cookies.txt',
80
+ 'headers': {
81
+ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'
82
+ },
83
+ 'nopostoverwrites': True,
84
  }
85
 
86
  with YoutubeDL(ydl_opts) as ydl:
 
99
  )
100
 
101
  except Exception as e:
102
+ return jsonify({'error': str(e)}), 500
103
 
104
  finally:
105
+ if 'file_name' in locals() and os.path.exists(file_name):
106
  os.remove(file_name)
107
 
108
  if __name__ == '__main__':