Hướng dẫn python download youtube video from url - python tải video youtube từ url

Ngày 22 tháng 12 năm 2021

YouTube đã trở thành nguồn cho video trên internet. Mặc dù có nhiều cách để tải xuống video YouTube, nhưng sử dụng Python là một trong những cách dễ nhất. Trong bài viết này, chúng tôi sẽ chỉ cho bạn cách sử dụng Python để tải xuống video YouTube.

Chúng tôi có thể sử dụng gói pytube để tải xuống video YouTube trong tập lệnh Python. Đó là một công cụ miễn phí bạn có thể cài đặt từ kho Pypi. Bạn cũng có thể chỉ định định dạng đầu ra [ví dụ: MP4] và độ phân giải [ví dụ: 720px] khi tải xuống video.

Nếu bạn đang tìm kiếm các ứng dụng thú vị hơn của lập trình Python, hãy kiểm tra, hãy tự động hóa những thứ nhàm chán bằng Python.Automate the Boring Stuff with Python.

Tải xuống video YouTube trong Python

Đây là cách tiếp cận từng bước để tải xuống video YouTube trong Python.

Bước I: Cài đặt pytube bằng PIP

pip install pytube

Bước II: Trong tập lệnh của bạn, nhập lớp YouTube từ gói PyTube.

from pytube import YouTube

Bước III: Tạo một đối tượng YouTube bỏ qua URL video

yt = YouTube[""]

Bước IV: Sử dụng phương thức bộ lọc để chỉ định định dạng tải xuống của video

mp4_files = yt.streams.filter[file_extension="mp4"]

Bước V: Nhận video bạn muốn bằng cách chỉ định độ phân giải

mp4_369p_files = mp4_files.get_by_resolution["360p"]

Bước VI: Lưu video đã tải xuống vào hệ thống tệp cục bộ

mp4_369p_files.download[""]

Đây là những gì kịch bản hoàn thành sẽ trông như thế nào. Tôi đã gói nó bằng một định nghĩa hàm chấp nhận URL và Outpath làm đối số.

from pytube import YouTube


def download_360p_mp4_videos[url: str, outpath: str = "./"]:

    yt = YouTube[url]

    yt.streams.filter[file_extension="mp4"].get_by_resolution["360p"].download[outpath]


if __name__ == "__main__":

    download_360p_mp4_videos[
        "//www.youtube.com/watch?v=JfVOs4VSpmA&t=4s&ab_channel=SonyPicturesEntertainment",
        "./trailers",
    ]

Mã trên sẽ tải xuống Trailer Spiderman: No Way Home và lưu nó trong một thư mục có tên là 'Trailers'.

Tạo CLI để tải xuống nhiều video hơn trong dấu nhắc lệnh.

Với một mẹo nhỏ, chúng tôi thậm chí có thể biến điều này thành một CLI. Đây là cách sử dụng Typer để chuyển đổi chức năng bạn đã viết thành CLI.

from pytube import YouTube
from typer import Typer

app = Typer[]


@app.command[]
def download_360p_mp4_videos[url: str, outpath: str = "./"]:
    yt = YouTube[url]
    yt.streams.filter[file_extension="mp4"].get_by_resolution["360p"].download[outpath]


if __name__ == "__main__":
    app[]

Bây giờ chúng tôi đã chuyển đổi chức năng của mình thành giao diện dòng lệnh chấp nhận tham số URL và lưu video vào hệ thống tập tin cục bộ. Đây là cách chạy này.

python get-yt.py "//www.youtube.com/watch?v=JfVOs4VSpmA&t=4s&ab_channel=SonyPicturesEntertainment"


YouTube là trang web chia sẻ video rất phổ biến. Tải xuống video từ YouTube là một công việc khó khăn. Tải xuống trình tải xuống và lấy video bằng cách sử dụng nó hoặc truy cập bất kỳ trang web nào khác tìm kiếm video và lưu trên máy tính của bạn. Sử dụng Python, nhiệm vụ này rất dễ dàng. Vài dòng mã sẽ tải video từ YouTube cho bạn. Đối với điều này, có một thư viện Python có tên là ‘Pytube. PyTube là một thư viện Python nhẹ, không phụ thuộc, được sử dụng để tải xuống video từ web.pytube không phải là thư viện gốc. Bạn cần cài đặt nó trước khi sử dụng nó. Cài đặt rất dễ dàng khi bạn có PIP. Trong dấu nhắc thiết bị đầu cuối hoặc lệnh, nhập lệnh sau để cài đặt pytube.
pytube is not the native library. You need to install it before using it. Installation is easy when you have pip. In the Terminal or Command Prompt, type the following command to install pytube.

pip install pytube

Trong trường hợp bạn không có PIP, hãy cài đặt nó như một thư viện bên ngoài.

Được đề xuất: Vui lòng thử cách tiếp cận của bạn trên {IDE} trước, trước khi chuyển sang giải pháp.{IDE} first, before moving on to the solution.

Tải xuống một video duy nhất

Thư viện Pytube làm cho video tải xuống rất dễ dàng. Tạo đối tượng của mô -đun YouTube bằng cách truyền liên kết làm tham số. Sau đó, nhận được phần mở rộng và độ phân giải thích hợp của video. Bạn có thể đặt tên của tệp là sự tiện lợi của bạn, trong một trường hợp khác, tên gốc sẽ được giữ. Sau đó, tải xuống tệp bằng chức năng tải xuống có một tham số là vị trí để tải xuống tệp.

Python3

from pytube import YouTube
0
from pytube import YouTube
1
from pytube import YouTube
2
from pytube import YouTube
3

from pytube import YouTube
4
from pytube import YouTube
5
from pytube import YouTube
6

from pytube import YouTube
7
from pytube import YouTube
8

from pytube import YouTube
from typer import Typer

app = Typer[]


@app.command[]
def download_360p_mp4_videos[url: str, outpath: str = "./"]:
    yt = YouTube[url]
    yt.streams.filter[file_extension="mp4"].get_by_resolution["360p"].download[outpath]


if __name__ == "__main__":
    app[]
9
python get-yt.py "//www.youtube.com/watch?v=JfVOs4VSpmA&t=4s&ab_channel=SonyPicturesEntertainment"
0
python get-yt.py "//www.youtube.com/watch?v=JfVOs4VSpmA&t=4s&ab_channel=SonyPicturesEntertainment"
1
python get-yt.py "//www.youtube.com/watch?v=JfVOs4VSpmA&t=4s&ab_channel=SonyPicturesEntertainment"
2

yt = YouTube[""]
3
from pytube import YouTube
8

from pytube import YouTube
9
yt = YouTube[""]
6
yt = YouTube[""]
7
yt = YouTube[""]
8
yt = YouTube[""]
9

python get-yt.py "//www.youtube.com/watch?v=JfVOs4VSpmA&t=4s&ab_channel=SonyPicturesEntertainment"
6
yt = YouTube[""]
0
from pytube import YouTube
5
python get-yt.py "//www.youtube.com/watch?v=JfVOs4VSpmA&t=4s&ab_channel=SonyPicturesEntertainment"
9

mp4_files = yt.streams.filter[file_extension="mp4"]
7
mp4_files = yt.streams.filter[file_extension="mp4"]
8
mp4_files = yt.streams.filter[file_extension="mp4"]
9

from pytube import YouTube
9
mp4_files = yt.streams.filter[file_extension="mp4"]
03015
mp4_files = yt.streams.filter[file_extension="mp4"]
2
mp4_files = yt.streams.filter[file_extension="mp4"]
3
yt = YouTube[""]
7
mp4_files = yt.streams.filter[file_extension="mp4"]
5
mp4_files = yt.streams.filter[file_extension="mp4"]
6

from pytube import YouTube
7
from pytube import YouTube
8

from pytube import YouTube
9
mp4_369p_files.download[""]
2

yt = YouTube[""]
3
from pytube import YouTube
8

from pytube import YouTube
9
mp4_369p_files = mp4_files.get_by_resolution["360p"]
0
from pytube import YouTube
5
mp4_369p_files = mp4_files.get_by_resolution["360p"]
2
mp4_369p_files = mp4_files.get_by_resolution["360p"]
3
mp4_369p_files = mp4_files.get_by_resolution["360p"]
4
mp4_369p_files = mp4_files.get_by_resolution["360p"]
5
mp4_369p_files = mp4_files.get_by_resolution["360p"]
3
mp4_369p_files = mp4_files.get_by_resolution["360p"]
444

yt = YouTube[""]
6
yt = YouTube[""]
7
from pytube import YouTube


def download_360p_mp4_videos[url: str, outpath: str = "./"]:

    yt = YouTube[url]

    yt.streams.filter[file_extension="mp4"].get_by_resolution["360p"].download[outpath]


if __name__ == "__main__":

    download_360p_mp4_videos[
        "//www.youtube.com/watch?v=JfVOs4VSpmA&t=4s&ab_channel=SonyPicturesEntertainment",
        "./trailers",
    ]
2
mp4_files = yt.streams.filter[file_extension="mp4"]
6

Tải xuống một tập tin mất một thời gian vì một lượng dữ liệu rất lớn đang được tải xuống từ web. Tùy thuộc vào tốc độ của kết nối, thời gian thực hiện để thực hiện chương trình khác nhau. Trong trường hợp bạn muốn tải xuống số lượng tệp, hãy đi với trường hợp tiếp theo.

Tải xuống nhiều video

Nhiệm vụ cơ bản của việc tải xuống nhiều video giống như tải xuống một video. Chúng tôi có thể sử dụng một vòng lặp để tải xuống video.

Python3

Trong đó, chúng tôi đã sử dụng một vòng lặp For để tải xuống nhiều tệp như được hiển thị. Người ta có thể sử dụng xử lý tệp để giữ tất cả các liên kết trong một tệp cần được tải xuống.

Tải xuống nhiều video bằng cách sử dụng xử lý tệp

Sử dụng xử lý tệp, chúng tôi có thể mở tệp có nhóm liên kết trong đó. Đi qua mọi liên kết của một tệp văn bản và áp dụng chương trình tải xuống video rất cơ bản được thực hiện ở đây. Ở đây, chúng tôi có một tệp văn bản có tên là Links Links_File.txt, có tất cả các liên kết cần tải xuống.

Python3

from pytube import YouTube
0
from pytube import YouTube
1
from pytube import YouTube
2
from pytube import YouTube
3

from pytube import YouTube
4
from pytube import YouTube
5
from pytube import YouTube
6

from pytube import YouTube
from typer import Typer

app = Typer[]


@app.command[]
def download_360p_mp4_videos[url: str, outpath: str = "./"]:
    yt = YouTube[url]
    yt.streams.filter[file_extension="mp4"].get_by_resolution["360p"].download[outpath]


if __name__ == "__main__":
    app[]
1
from pytube import YouTube
5
from pytube import YouTube
from typer import Typer

app = Typer[]


@app.command[]
def download_360p_mp4_videos[url: str, outpath: str = "./"]:
    yt = YouTube[url]
    yt.streams.filter[file_extension="mp4"].get_by_resolution["360p"].download[outpath]


if __name__ == "__main__":
    app[]
3
yt = YouTube[""]
7
from pytube import YouTube
from typer import Typer

app = Typer[]


@app.command[]
def download_360p_mp4_videos[url: str, outpath: str = "./"]:
    yt = YouTube[url]
    yt.streams.filter[file_extension="mp4"].get_by_resolution["360p"].download[outpath]


if __name__ == "__main__":
    app[]
5
from pytube import YouTube
from typer import Typer

app = Typer[]


@app.command[]
def download_360p_mp4_videos[url: str, outpath: str = "./"]:
    yt = YouTube[url]
    yt.streams.filter[file_extension="mp4"].get_by_resolution["360p"].download[outpath]


if __name__ == "__main__":
    app[]
6
from pytube import YouTube
from typer import Typer

app = Typer[]


@app.command[]
def download_360p_mp4_videos[url: str, outpath: str = "./"]:
    yt = YouTube[url]
    yt.streams.filter[file_extension="mp4"].get_by_resolution["360p"].download[outpath]


if __name__ == "__main__":
    app[]
7
mp4_files = yt.streams.filter[file_extension="mp4"]
6

from pytube import YouTube
from typer import Typer

app = Typer[]


@app.command[]
def download_360p_mp4_videos[url: str, outpath: str = "./"]:
    yt = YouTube[url]
    yt.streams.filter[file_extension="mp4"].get_by_resolution["360p"].download[outpath]


if __name__ == "__main__":
    app[]
9
python get-yt.py "//www.youtube.com/watch?v=JfVOs4VSpmA&t=4s&ab_channel=SonyPicturesEntertainment"
0
python get-yt.py "//www.youtube.com/watch?v=JfVOs4VSpmA&t=4s&ab_channel=SonyPicturesEntertainment"
1
python get-yt.py "//www.youtube.com/watch?v=JfVOs4VSpmA&t=4s&ab_channel=SonyPicturesEntertainment"
2

from pytube import YouTube
9
from pytube import YouTube
7
from pytube import YouTube
8

python get-yt.py "//www.youtube.com/watch?v=JfVOs4VSpmA&t=4s&ab_channel=SonyPicturesEntertainment"
6
yt = YouTube[""]
0
from pytube import YouTube
5
python get-yt.py "//www.youtube.com/watch?v=JfVOs4VSpmA&t=4s&ab_channel=SonyPicturesEntertainment"
9

from pytube import YouTube
9
yt = YouTube[""]
3
from pytube import YouTube
8

python get-yt.py "//www.youtube.com/watch?v=JfVOs4VSpmA&t=4s&ab_channel=SonyPicturesEntertainment"
6
yt = YouTube[""]
6
yt = YouTube[""]
7
yt = YouTube[""]
8
mp4_files = yt.streams.filter[file_extension="mp4"]
9

from pytube import YouTube
9
mp4_files = yt.streams.filter[file_extension="mp4"]
03015
mp4_files = yt.streams.filter[file_extension="mp4"]
2
mp4_files = yt.streams.filter[file_extension="mp4"]
3
yt = YouTube[""]
7
mp4_files = yt.streams.filter[file_extension="mp4"]
5
mp4_files = yt.streams.filter[file_extension="mp4"]
6

from pytube import YouTube
9
mp4_369p_files = mp4_files.get_by_resolution["360p"]
0
from pytube import YouTube
5
mp4_369p_files = mp4_files.get_by_resolution["360p"]
2
mp4_369p_files = mp4_files.get_by_resolution["360p"]
3
mp4_369p_files = mp4_files.get_by_resolution["360p"]
4
mp4_369p_files = mp4_files.get_by_resolution["360p"]
5
mp4_369p_files = mp4_files.get_by_resolution["360p"]
3
mp4_369p_files = mp4_files.get_by_resolution["360p"]
444

from pytube import YouTube
9
from pytube import YouTube
7
from pytube import YouTube
8

python get-yt.py "//www.youtube.com/watch?v=JfVOs4VSpmA&t=4s&ab_channel=SonyPicturesEntertainment"
6
mp4_369p_files.download[""]
2

from pytube import YouTube
9
yt = YouTube[""]
3
from pytube import YouTube
8

python get-yt.py "//www.youtube.com/watch?v=JfVOs4VSpmA&t=4s&ab_channel=SonyPicturesEntertainment"
6
yt = YouTube[""]
6
yt = YouTube[""]
7
mp4_369p_files.download[""]
8
mp4_files = yt.streams.filter[file_extension="mp4"]
6

yt = YouTube[""]
6
yt = YouTube[""]
7
from pytube import YouTube


def download_360p_mp4_videos[url: str, outpath: str = "./"]:

    yt = YouTube[url]

    yt.streams.filter[file_extension="mp4"].get_by_resolution["360p"].download[outpath]


if __name__ == "__main__":

    download_360p_mp4_videos[
        "//www.youtube.com/watch?v=JfVOs4VSpmA&t=4s&ab_channel=SonyPicturesEntertainment",
        "./trailers",
    ]
2
mp4_files = yt.streams.filter[file_extension="mp4"]
6

Điểm quan trọng:

  1. Hãy chắc chắn rằng bạn được kết nối với Internet để tải xuống các video. Nếu không, nó sẽ gây ra lỗi.
  2. Don Tiết sử dụng hàm set_filename [] trong bất kỳ vòng lặp. Trong trường hợp này, chỉ có một video sẽ được tải xuống.
  3. Bạn có thể sửa đổi tên mỗi khi bằng một mảng tên khác.
  4. Sự gián đoạn kết nối ở giữa cũng sẽ gây ra lỗi và video sẽ không được tải xuống trong trường hợp đó.

Bài viết này được đóng góp bởi Rishabh Bansal. Nếu bạn thích GeekSforGeeks và muốn đóng góp, bạn cũng có thể viết một bài viết bằng cách sử dụng PROPTENT.GeekSforGeeks.org hoặc gửi bài viết của bạn đến. Xem bài viết của bạn xuất hiện trên trang chính của GeekSforGeek và giúp các chuyên viên máy tính khác.Rishabh Bansal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks.

Vui lòng viết nhận xét nếu bạn tìm thấy bất cứ điều gì không chính xác, hoặc bạn muốn chia sẻ thêm thông tin về chủ đề được thảo luận ở trên.


Bài Viết Liên Quan

Chủ Đề