Implementing LinkedIn Downloader with yt-dlp

Welcome back to our journey in video scraping! In previous lessons, you've learned how to transcribe videos using the Whisper API and FFmpeg, as well as download videos from public Google Drive links. In this lesson, we'll take things further by downloading videos from LinkedIn using yt-dlp. This browser-based tool simplifies accessing video content across multiple platforms.

What You'll Learn

In this lesson, you will:

  • Identify and validate a range of LinkedIn URLs.
  • Discover how yt-dlp facilitates downloading from LinkedIn.
  • Manage temporary files and address potential legal concerns when downloading videos.
Understanding LinkedIn Video Downloading

Our objective is to leverage yt-dlp, initially designed for YouTube, but flexible enough for use with LinkedIn. The key is recognizing valid LinkedIn URLs and downloading videos efficiently.

LinkedIn URLs can be in formats such as:

  • Full length: https://www.linkedin.com/feed/update/urn:li:activity:VIDEO_ID
  • Post based: https://www.linkedin.com/posts/USERNAME_activity-VIDEO_ID

Understanding these structures is crucial for initiating the download process.

Detecting LinkedIn URLs

We'll start by verifying if a URL belongs to LinkedIn:

from urllib.parse import urlparse

class LinkedInService:
    @staticmethod
    def is_linkedin_url(url):
        parsed = urlparse(url)
        valid_paths = [
            '/feed/update/urn:li:activity:',  # Existing format
            '/posts/'  # New format to support
        ]
        return 'linkedin.com' in parsed.netloc and any(path in parsed.path for path in valid_paths)

This function checks for linkedin.com in the URL's net location and confirms if a recognizable path is present, ensuring accurate URL validation.

Downloading Videos with yt-dlp

Once the URL is validated, we proceed with the download using yt-dlp:

import os
import tempfile
import yt_dlp

@staticmethod
def download_video(url):
    print("Downloading LinkedIn video...")
    
    temp_dir = tempfile.mkdtemp()
    output_template = os.path.join(temp_dir, '%(title)s.%(ext)s')

    try:
        ydl_opts = {
            'format': 'mp4',
            'outtmpl': output_template,
            'quiet': True,
            'progress_hooks': [lambda d: print(f"Status: {d['status']}")]
        }

        with yt_dlp.YoutubeDL(ydl_opts) as ydl:
            ydl.download([url])

        files = os.listdir(temp_dir)
        if not files:
            raise Exception("No file downloaded")

        return os.path.join(temp_dir, files[0])
    except Exception as e:
        print(f"Error downloading video: {e}")
        raise ValueError(f"Failed to download LinkedIn video: {str(e)}")

Here's how it works:

  • A temporary directory holds the video.
  • The output is organized with the video's title and format (mp4).
  • yt-dlp options specify format, templating, quiet download, and hooks to track progress.
Why It Matters

Mastering LinkedIn video downloads with yt-dlp enables the collection of educational videos, supports offline access, and aids in backing up personal content. Always be aware of potential legal issues, ensuring compliance with terms of service and copyright laws.

Now that you understand the downloader's potential, take the upcoming practice section as an opportunity to solidify your knowledge with hands-on tasks.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal