File size: 3,407 Bytes
d66c44d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
from flask import Flask, request, jsonify, send_file, render_template
from yt_dlp import YoutubeDL
import os

app = Flask(__name__)

@app.route('/')
def home():
    return render_template('index.html')

@app.route('/get-info', methods=['POST'])
def get_info():
    data = request.json
    url = data.get('url')

    if not url:
        return jsonify({'error': 'URL is required'}), 400

    try:
        ydl_opts = {
            'cookiefile': 'www.youtube.com_cookies.txt'
        }
        
        with YoutubeDL(ydl_opts) as ydl:
            info = ydl.extract_info(url, download=False)
            return jsonify({
                'title': info['title'],
                'thumbnail': info.get('thumbnail'),
                'duration': info.get('duration'),
                'channel': info.get('channel')
            })

    except Exception as e:
        return jsonify({'error': str(e)}), 500

@app.route('/download', methods=['POST'])
def download():
    data = request.json
    url = data.get('url')
    format_type = data.get('format', 'mp3')
    quality = data.get('quality', 'best')

    if not url:
        return jsonify({'error': 'URL is required'}), 400

    try:
        if format_type == 'mp3':
            ydl_opts = {
                'format': 'bestaudio/best',
                'outtmpl': '%(title)s.%(ext)s',
                'cookiefile': 'www.youtube.com_cookies.txt',
                'postprocessors': [{
                    'key': 'FFmpegExtractAudio',
                    'preferredcodec': 'mp3',
                    'preferredquality': '192',
                }],
            }
        else: 
            quality_map = {
                    '144': 'bv*[height=144][ext=mp4]+ba[ext=m4a]/b[height=144][ext=mp4]',
                    '240': 'bv*[height=240][ext=mp4]+ba[ext=m4a]/b[height=240][ext=mp4]',
                    '360': 'bv*[height=360][ext=mp4]+ba[ext=m4a]/b[height=360][ext=mp4]',
                    '480': 'bv*[height=480][ext=mp4]+ba[ext=m4a]/b[height=480][ext=mp4]',
                    '720': 'bv*[height=720][ext=mp4]+ba[ext=m4a]/b[height=720][ext=mp4]',
                    '1080': 'bv*[height=1080][ext=mp4]+ba[ext=m4a]/b[height=1080][ext=mp4]',
                    'best': 'bv*[ext=mp4]+ba[ext=m4a]/b[ext=mp4]'
                }

            selected_quality = quality_map.get(quality, quality_map['best'])
            
            ydl_opts = {
                'format': selected_quality,
                'outtmpl': '%(title)s.%(ext)s',
                'merge_output_format': 'mp4',
                'cookiefile': 'www.youtube.com_cookies.txt',
            }

        with YoutubeDL(ydl_opts) as ydl:
            info = ydl.extract_info(url, download=True)
            
            if format_type == 'mp3':
                file_name = ydl.prepare_filename(info).rsplit(".", 1)[0] + ".mp3"
            else:
                file_name = ydl.prepare_filename(info)

        return send_file(
            file_name,
            as_attachment=True,
            download_name=os.path.basename(file_name),
            mimetype='video/mp4' if format_type == 'mp4' else 'audio/mp3'
        )

    except Exception as e:
        return jsonify({'error': str(e)}), 500

    finally:
        if 'file_name' in locals() and os.path.exists(file_name):
            os.remove(file_name)

if __name__ == '__main__':
       app.run(host='0.0.0.0', port=7860, debug=True,threaded=True)