hansaka1 commited on
Commit
6ccdea2
·
verified ·
1 Parent(s): 5557d6a

Update srv.py

Browse files
Files changed (1) hide show
  1. srv.py +32 -12
srv.py CHANGED
@@ -1,6 +1,7 @@
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
 
@@ -42,31 +43,50 @@ def download_video():
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,
60
- download_name=os.path.basename(file_name)
61
  )
62
 
63
  except Exception as e:
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)
 
1
  from flask import Flask, request, jsonify, send_file, render_template
2
  from yt_dlp import YoutubeDL
3
  import os
4
+ import subprocess
5
 
6
  app = Flask(__name__)
7
 
 
43
  return jsonify({'error': 'URL is required'}), 400
44
 
45
  try:
46
+ # Download video and audio streams
47
  ydl_opts = {
48
+ 'format': 'bestvideo+bestaudio/best', # Download the best video and audio
49
+ 'outtmpl': '%(title)s.%(ext)s', # Save file with title as filename
50
+ 'cookiefile': 'www.youtube.com_cookies.txt', # Use cookies if needed
51
+ 'noplaylist': True, # Avoid downloading playlists
52
  }
53
 
54
  with YoutubeDL(ydl_opts) as ydl:
55
  info = ydl.extract_info(url, download=True)
56
+ video_file = ydl.prepare_filename(info)
57
 
58
+ # Set the output file for merged video and audio
59
+ output_file = video_file.rsplit('.', 1)[0] + '_merged.mp4'
60
+
61
+ # Use FFmpeg to merge audio and video files
62
+ video_file_path = video_file
63
+ audio_file_path = video_file.rsplit('.', 1)[0] + '.m4a' # Assuming audio is in .m4a format
64
+
65
+ # Merge audio and video using FFmpeg
66
+ ffmpeg_command = [
67
+ 'ffmpeg', '-i', video_file_path, '-i', audio_file_path,
68
+ '-c:v', 'copy', '-c:a', 'aac', '-strict', 'experimental', output_file
69
+ ]
70
+ subprocess.run(ffmpeg_command, check=True)
71
+
72
+ # Remove the original separate video and audio files to save space
73
+ os.remove(video_file_path)
74
+ os.remove(audio_file_path)
75
+
76
+ # Send the merged video file to the client
77
  return send_file(
78
+ output_file,
79
  as_attachment=True,
80
+ download_name=os.path.basename(output_file)
81
  )
82
 
83
  except Exception as e:
84
  return jsonify({'error': str(e)}), 500
85
 
86
  finally:
87
+ # Clean up the merged file after sending it
88
+ if 'output_file' in locals() and os.path.exists(output_file):
89
+ os.remove(output_file)
90
 
91
  if __name__ == '__main__':
92
+ app.run(host='0.0.0.0', port=7860, debug=True)