vikarshana commited on
Commit
32ac84b
·
verified ·
1 Parent(s): e0e35d9

Update srv.py

Browse files
Files changed (1) hide show
  1. srv.py +55 -84
srv.py CHANGED
@@ -1,107 +1,78 @@
1
- """
2
- ------------------------------------------------------------
3
- YouTube Video & Audio Downloader API
4
- ------------------------------------------------------------
5
- ✨ Developed by: Mr. Asitha
6
- ✅ Contact: +94743381623
7
- 📅 Created: 2025-03-18
8
- 🔗 Join WhatsApp Channel: https://whatsapp.com/channel/0029VaeyMWv3QxRu4hA6c33Z
9
- 🚀 Program: Flask API for Downloading YouTube Videos & MP3
10
- ------------------------------------------------------------
11
- """
12
-
13
- from flask import Flask, request, jsonify, send_file, render_template
14
- from yt_dlp import YoutubeDL
15
  import os
16
 
17
  app = Flask(__name__)
18
 
19
- @app.route('/')
20
- def home():
21
- return render_template('index.html')
22
-
23
- @app.route('/get-info', methods=['GET', 'POST'])
24
- def get_info():
25
  data = request.json
26
  url = data.get('url')
27
-
28
- if not url:
29
- return jsonify({'error': 'URL is required'}), 400
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
  try:
32
- ydl_opts = {
33
- 'cookiefile': 'www.youtube.com_cookies.txt'
34
- }
35
-
36
- with YoutubeDL(ydl_opts) as ydl:
37
- info = ydl.extract_info(url, download=False)
38
- return jsonify({
39
- 'title': info['title'],
40
- 'thumbnail': info.get('thumbnail'),
41
- 'duration': info.get('duration'),
42
- 'channel': info.get('channel')
43
- })
44
-
45
  except Exception as e:
46
  return jsonify({'error': str(e)}), 500
47
 
48
- @app.route('/download', methods=['GET', 'POST'])
49
- def download_media():
50
- data = request.json
51
- url = data.get('url')
52
- format_type = data.get('format_type', 'mp3')
53
- quality = str(data.get('quality', 'best'))
54
 
55
  if not url:
56
  return jsonify({'error': 'URL is required'}), 400
57
 
58
  try:
59
- if format_type == 'mp4':
60
- quality_map = {
61
- '144': 'bv*[height=144][ext=mp4]+ba[ext=m4a]/b[height=144][ext=mp4]',
62
- '240': 'bv*[height=240][ext=mp4]+ba[ext=m4a]/b[height=240][ext=mp4]',
63
- '360': 'bv*[height=360][ext=mp4]+ba[ext=m4a]/b[height=360][ext=mp4]',
64
- '480': 'bv*[height=480][ext=mp4]+ba[ext=m4a]/b[height=480][ext=mp4]',
65
- '720': 'bv*[height=720][ext=mp4]+ba[ext=m4a]/b[height=720][ext=mp4]',
66
- '1080': 'bv*[height=1080][ext=mp4]+ba[ext=m4a]/b[height=1080][ext=mp4]',
67
- 'best': 'bv*[ext=mp4]+ba[ext=m4a]/b[ext=mp4]'
68
- }
69
 
70
- ydl_opts = {
71
- 'format': quality_map.get(quality, quality_map['best']),
72
- 'merge_output_format': 'mp4',
73
- 'outtmpl': '%(title)s.%(ext)s',
74
- 'cookiefile': 'www.youtube.com_cookies.txt'
75
- }
76
- else:
77
- ydl_opts = {
78
- 'format': 'bestaudio',
79
- 'outtmpl': '%(title)s.%(ext)s',
80
- 'cookiefile': 'www.youtube.com_cookies.txt',
81
- 'postprocessors': [{
82
- 'key': 'FFmpegExtractAudio',
83
- 'preferredcodec': 'mp3',
84
- 'preferredquality': '64',
85
- }],
86
  }
87
-
88
- with YoutubeDL(ydl_opts) as ydl:
89
- info = ydl.extract_info(url, download=True)
90
- file_ext = "mp4" if format_type == "mp4" else "mp3"
91
- file_name = ydl.prepare_filename(info).rsplit(".", 1)[0] + f".{file_ext}"
92
-
93
- return send_file(
94
- file_name,
95
- as_attachment=True,
96
- download_name=os.path.basename(file_name)
97
- )
98
 
99
  except Exception as e:
100
  return jsonify({'error': str(e)}), 500
101
 
102
- finally:
103
- if 'file_name' in locals() and os.path.exists(file_name):
104
- os.remove(file_name)
105
-
106
  if __name__ == '__main__':
107
- app.run(host='0.0.0.0', port=7860, debug=True, threaded=True)
 
1
+ from flask import Flask, request, jsonify, send_file
2
+ import yt_dlp
 
 
 
 
 
 
 
 
 
 
 
 
3
  import os
4
 
5
  app = Flask(__name__)
6
 
7
+ @app.route('/download', methods=['GET', 'POST'])
8
+ def download_video():
 
 
 
 
9
  data = request.json
10
  url = data.get('url')
11
+ quality = data.get('quality')
12
+ cookies = data.get('cookies')
13
+
14
+ if not url or not quality:
15
+ return jsonify({'error': 'URL and quality are required'}), 400
16
+
17
+ # Define quality options
18
+ quality_options = {
19
+ '144': '144p',
20
+ '240': '240p',
21
+ '360': '360p',
22
+ '480': '480p',
23
+ '720': '720p',
24
+ '1080': '1080p'
25
+ }
26
+
27
+ if quality not in quality_options:
28
+ return jsonify({'error': 'Invalid quality option'}), 400
29
+
30
+ # Set cookies for yt-dlp
31
+ cookie_file = 'cookies.txt'
32
+ with open(cookie_file, 'w') as f:
33
+ f.write(cookies)
34
+
35
+ # Download video options
36
+ ydl_opts = {
37
+ 'format': f'bestvideo[height<={quality}]',
38
+ 'outtmpl': '%(title)s.%(ext)s',
39
+ 'cookiefile': cookie_file,
40
+ 'noplaylist': True,
41
+ }
42
 
43
  try:
44
+ with yt_dlp.YoutubeDL(ydl_opts) as ydl:
45
+ info = ydl.extract_info(url, download=True)
46
+ filename = ydl.prepare_filename(info)
 
 
 
 
 
 
 
 
 
 
47
  except Exception as e:
48
  return jsonify({'error': str(e)}), 500
49
 
50
+ return send_file(filename, as_attachment=True)
51
+
52
+ @app.route('/info', methods=['GET', 'POST'])
53
+ def get_video_info():
54
+ url = request.args.get('url')
 
55
 
56
  if not url:
57
  return jsonify({'error': 'URL is required'}), 400
58
 
59
  try:
60
+ ydl_opts = {
61
+ 'cookiefile': 'cookies.txt' # Optional: Use cookies if needed
62
+ }
 
 
 
 
 
 
 
63
 
64
+ with yt_dlp.YoutubeDL(ydl_opts) as ydl:
65
+ info = ydl.extract_info(url, download=False)
66
+ video_info = {
67
+ 'title': info.get('title'),
68
+ 'thumbnail': info.get('thumbnail'),
69
+ 'duration': info.get('duration'),
70
+ 'channel': info.get('channel')
 
 
 
 
 
 
 
 
 
71
  }
72
+ return jsonify(video_info)
 
 
 
 
 
 
 
 
 
 
73
 
74
  except Exception as e:
75
  return jsonify({'error': str(e)}), 500
76
 
 
 
 
 
77
  if __name__ == '__main__':
78
+ app.run(debug=True)