Cài Python https://www.python.org/downloads
tải ffmpeg https://cloud.trungdungmedia.com/index.php/s/Yryw2YGgZeTAeKe
pip install yt-dlp
pip install pyinstaller
--------------------------------------------------------------------------
tạo file tai_video_youtube.py
==========================================================================
import os
import subprocess
def download_video():
while True:
url = input("\n🔗 Nhập link YouTube vào đây (nhấn Enter để tải về): ").strip()
if url.lower() == "thoát":
print("👋 Kết thúc chương trình.")
break
if not url.startswith("http"):
print("❌ Link không hợp lệ. Vui lòng thử lại.")
continue
command = [
"python", "-m", "yt_dlp",
"-f", "bestvideo[height<=1080]+bestaudio/best[height<=1080]",
"--merge-output-format", "mp4",
"-o", "%(title)s.%(ext)s",
url
]
print("📥 Đang tải video...")
try:
subprocess.run(command)
print("✅ Tải xong.")
except Exception as e:
print(f"⚠️ Lỗi khi tải: {e}")
if __name__ == "__main__":
print("=== Trình tải video YouTube 1080p ===")
download_video()
==========================================================================
tạo file tai_mp3_tu_youtube.py
--------------------------------------------------------------------------
import os
import subprocess
import glob
def download_and_convert_to_mp3(url):
print("📥 Đang tải audio...")
command = [
"python", "-m", "yt_dlp",
"-f", "bestaudio",
"--extract-audio",
"--audio-format", "mp3",
"--audio-quality", "0", # 0 = best (320kbps)
"-o", "%(title)s.%(ext)s",
url
]
try:
subprocess.run(command)
print("🎵 Đã chuyển đổi và lưu dưới dạng MP3.")
except Exception as e:
print(f"⚠️ Lỗi khi tải hoặc chuyển đổi: {e}")
def main():
print("=== 🎧 Trình tải MP3 từ YouTube ===")
while True:
url = input("\n🔗 Nhập link YouTube vào đây (nhấn Enter để tải về): ").strip()
if url.lower() == "thoát":
print("👋 Kết thúc chương trình.")
break
if not url.startswith("http"):
print("❌ Link không hợp lệ. Vui lòng thử lại.")
continue
download_and_convert_to_mp3(url)
if __name__ == "__main__":
main()
==========================================================================
chạy lệnh để convert thành file .exe
--------------------------------------------------------------------------
python -m PyInstaller --onefile --console tai_video_youtube.py
2025-05-01