Youtube Video Trim & Download

Thilinacw

Well-known member
  • Oct 3, 2011
    1,347
    606
    113
    Colombo
    මචන්ලා,
    පහසුවෙන් youtube video එකක අවශ්‍ය කොටසක් download කරගන්න ක්‍රමයක් දන්නවද? සම්පුර්ණ එකම ඩවුන්ලෝඩ් නොකර. තියෙන වෙබ්සයිට් වලින් හරියට වැඩේ වෙන් නෑ.. ඉස්සර නම් ගොඩක් වෙබ්සයිට් වැඩ කරා. Thanks.
     

    rukshrulz

    Well-known member
  • Jul 16, 2013
    9,815
    16,649
    113
    Colombo
    Below is a Google Colab script that demonstrates how to download a YouTube video, trim it to a specific time range, and process it without saving it locally on your device. This is achieved using pytube for downloading and moviepy for trimming.

    Code:
    # Install required libraries
    !pip install pytube moviepy
    
    # Import necessary modules
    from pytube import YouTube
    from moviepy.video.io.VideoFileClip import VideoFileClip
    from IPython.display import Video
    
    # Function to download and process the YouTube video
    def download_and_trim_youtube_video(url, start_time, end_time):
        # Step 1: Download the YouTube video
        yt = YouTube(url)
        video_stream = yt.streams.filter(progressive=True, file_extension='mp4').first()
        video_path = video_stream.download(filename="temp_video.mp4")
        print(f"Video downloaded: {video_path}")
        
        # Step 2: Trim the video
        with VideoFileClip(video_path) as video_clip:
            trimmed_clip = video_clip.subclip(start_time, end_time)
            trimmed_clip.write_videofile("trimmed_video.mp4", codec="libx264")
        
        print("Video trimmed successfully and saved as 'trimmed_video.mp4'")
        return "trimmed_video.mp4"
    
    # Provide the YouTube video URL and trim start/end times (in seconds)
    video_url = input("Enter the YouTube video URL: ")
    start = int(input("Enter the start time in seconds: "))
    end = int(input("Enter the end time in seconds: "))
    
    # Process the video
    output_path = download_and_trim_youtube_video(video_url, start, end)
    
    # Display the trimmed video in Colab
    print("Displaying the trimmed video:")
    Video(output_path)

    1. Copy and paste this code into a Google Colab notebook.
    2. Run the script and enter the YouTube video URL, start time, and end time when prompted.
    3. The script will:
      • Download the YouTube video using pytube.
      • Trim the video to the specified start and end times using moviepy.
      • Save the trimmed video to the Colab environment and display it.
    This method avoids downloading the processed video to your local device unless you choose to manually download it later.
     

    rockmon

    Well-known member
  • Jul 31, 2017
    3,453
    3,841
    113
    මචන්ලා,
    පහසුවෙන් youtube video එකක අවශ්‍ය කොටසක් download කරගන්න ක්‍රමයක් දන්නවද? සම්පුර්ණ එකම ඩවුන්ලෝඩ් නොකර. තියෙන වෙබ්සයිට් වලින් හරියට වැඩේ වෙන් නෑ.. ඉස්සර නම් ගොඩක් වෙබ්සයිට් වැඩ කරා. Thanks.
    https://9c77-45-121-88-144.ngrok-free.app
     

    Thilinacw

    Well-known member
  • Oct 3, 2011
    1,347
    606
    113
    Colombo
    Below is a Google Colab script that demonstrates how to download a YouTube video, trim it to a specific time range, and process it without saving it locally on your device. This is achieved using pytube for downloading and moviepy for trimming.

    Code:
    # Install required libraries
    !pip install pytube moviepy
    
    # Import necessary modules
    from pytube import YouTube
    from moviepy.video.io.VideoFileClip import VideoFileClip
    from IPython.display import Video
    
    # Function to download and process the YouTube video
    def download_and_trim_youtube_video(url, start_time, end_time):
        # Step 1: Download the YouTube video
        yt = YouTube(url)
        video_stream = yt.streams.filter(progressive=True, file_extension='mp4').first()
        video_path = video_stream.download(filename="temp_video.mp4")
        print(f"Video downloaded: {video_path}")
       
        # Step 2: Trim the video
        with VideoFileClip(video_path) as video_clip:
            trimmed_clip = video_clip.subclip(start_time, end_time)
            trimmed_clip.write_videofile("trimmed_video.mp4", codec="libx264")
       
        print("Video trimmed successfully and saved as 'trimmed_video.mp4'")
        return "trimmed_video.mp4"
    
    # Provide the YouTube video URL and trim start/end times (in seconds)
    video_url = input("Enter the YouTube video URL: ")
    start = int(input("Enter the start time in seconds: "))
    end = int(input("Enter the end time in seconds: "))
    
    # Process the video
    output_path = download_and_trim_youtube_video(video_url, start, end)
    
    # Display the trimmed video in Colab
    print("Displaying the trimmed video:")
    Video(output_path)

    1. Copy and paste this code into a Google Colab notebook.
    2. Run the script and enter the YouTube video URL, start time, and end time when prompted.
    3. The script will:
      • Download the YouTube video using pytube.
      • Trim the video to the specified start and end times using moviepy.
      • Save the trimmed video to the Colab environment and display it.
    This method avoids downloading the processed video to your local device unless you choose to manually download it later.
    Thanks Brothers ❤️