DEV Community

Chemical Engineer
Chemical Engineer

Posted on

Guide to Download Public Instagram Reels with Python

Instagram Reels have become one of the most popular ways to share short, engaging videos. While Instagram allows users to view and share these videos within the app, downloading them in high quality for offline access isn't a built-in feature. This article provides a step-by-step guide on how to download public Instagram Reels using Python and the powerful tool, yt-dlp.

Let's dive in!


Overview

This tutorial demonstrates how to:

  1. Install yt-dlp, a versatile tool for downloading videos from various platforms.
  2. Use Python to automate the process of downloading Instagram Reels in the best quality.
  3. Save downloaded videos to your local device.

Prerequisites

Before you begin, ensure that:

  • You have Python installed on your system.
  • You're familiar with Google Colab (optional, but recommended for a streamlined experience).

Step 1: Installing yt-dlp

The first step is installing yt-dlp, a modern alternative to youtube-dl. It supports a wide range of platforms, including Instagram, and offers superior performance.

!pip install -q yt-dlp
Enter fullscreen mode Exit fullscreen mode

This command installs yt-dlp in your Python environment. The -q flag ensures that installation messages are minimal.


Step 2: Configuring the Script

Below is the Python script designed to download public Instagram Reels:

Code Explanation

import yt_dlp
from google.colab import files
import glob
Enter fullscreen mode Exit fullscreen mode
  • yt_dlp: The main library used for downloading videos.
  • files: Part of Google Colab, this allows downloading files to your local machine.
  • glob: Helps in locating files in the directory.

Downloading the Reel

The download_instagram_reel function is the heart of this script. It uses yt-dlp to fetch and save the Instagram Reel in the highest quality.

def download_instagram_reel(url):
    ydl_opts = {
        'format': 'bestvideo+bestaudio/best',  # Ensures highest quality
        'outtmpl': '/content/%(title)s.%(ext)s',  # Sets output file path
        'merge_output_format': 'mp4',  # Merges video and audio into MP4
        'quiet': False,  # Shows download progress
    }

    try:
        with yt_dlp.YoutubeDL(ydl_opts) as ydl:
            print("πŸ“₯ Downloading the reel...")
            ydl.download([url])
            print("βœ… Download completed successfully!")
    except Exception as e:
        print(f"❌ Error: {e}")
Enter fullscreen mode Exit fullscreen mode

Downloading to Local Device

Once the video is downloaded, the download_to_device function prepares it for transfer to your local system.

def download_to_device():
    video_files = glob.glob('/content/*.mp4')  # Search for MP4 files
    if video_files:
        for video in video_files:
            print(f"πŸ“‚ Preparing to download: {video}")
            files.download(video)
        print("πŸ“¦ Download ready!")
    else:
        print("⚠️ No video found. Please check the URL and try again.")
Enter fullscreen mode Exit fullscreen mode

Step 3: Inputting the Instagram Reel URL

To download a Reel, you need its public URL. The script prompts you to input this URL:

reel_url = input("πŸ”— Enter the public Instagram Reel URL: ")
Enter fullscreen mode Exit fullscreen mode

Step 4: Downloading and Saving

The script downloads the video and makes it available for download to your local machine:

download_instagram_reel(reel_url)
download_to_device()
Enter fullscreen mode Exit fullscreen mode

How to Use the Script

  1. Run the Script in Google Colab: Paste the script into a Google Colab notebook and run it cell by cell.
  2. Input the Reel URL: Enter the public URL of the Instagram Reel when prompted.
  3. Download the Reel: Wait for the script to download the video. After completion, click the download link to save it locally.

Key Features of This Script

  • High Quality: Ensures the best video and audio quality.
  • User-Friendly: Minimal input required; just provide the Reel URL.
  • Platform-Independent: Works seamlessly on Google Colab or any Python-supported environment.

Important Notes

  • Public Reels Only: The script works only for publicly accessible Instagram Reels. Ensure the Reel isn't private.
  • Respect Copyright: Always adhere to Instagram's terms of service and download content only with proper permissions.

For Non-Tech People Copy this code directly:

# πŸ“¦ Install yt-dlp for downloading Instagram Reels
!pip install -q yt-dlp

# πŸ”½ Download Public Instagram Reels in Highest Quality
import yt_dlp
from google.colab import files
import glob

def download_instagram_reel(url):
    # Configure yt-dlp to download the best available quality
    ydl_opts = {
        'format': 'bestvideo+bestaudio/best',  # Highest video and audio quality
        'outtmpl': '/content/%(title)s.%(ext)s',  # Save to Colab's content directory
        'merge_output_format': 'mp4',  # Output format
        'quiet': False,  # Show download progress
    }

    try:
        with yt_dlp.YoutubeDL(ydl_opts) as ydl:
            print("πŸ“₯ Downloading the reel...")
            ydl.download([url])
            print("βœ… Download completed successfully!")
    except Exception as e:
        print(f"❌ Error: {e}")

def download_to_device():
    # Search for downloaded video files in Colab's content folder
    video_files = glob.glob('/content/*.mp4')
    if video_files:
        for video in video_files:
            print(f"πŸ“‚ Preparing to download: {video}")
            files.download(video)
        print("πŸ“¦ Download ready!")
    else:
        print("⚠️ No video found. Please check the URL and try again.")

# πŸ”— Input the Public Instagram Reel URL
reel_url = input("πŸ”— Enter the public Instagram Reel URL: ")

# ⬇️ Download Reel
download_instagram_reel(reel_url)

# πŸ“₯ Download the file to your local device
download_to_device()

Enter fullscreen mode Exit fullscreen mode

Conclusion

With this Python script, downloading public Instagram Reels is a breeze. It’s a great tool for content creators, educators, or anyone who wants offline access to these engaging videos. Experiment with the script, and customize it to suit your needs.

Feel free to share this article with others who might find it helpful. Happy downloading!

Credits:

ChemEnggCalc - Learn Chemical Engineers Calculations with Tools & Tech

Learn Chemical Engineers Calculations with Tools & Tech

favicon chemenggcalc.com

Top comments (0)