youtube downloader

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_URL_LENGTH 256
#define MAX_FILENAME_LENGTH 256

void downloadVideo(const char *url);

int main() {
    char url[MAX_URL_LENGTH];

    printf("Enter YouTube video URL: ");
    fgets(url, sizeof(url), stdin);

    // Remove newline character from the URL
    url[strcspn(url, "\n")] = '\0';

    downloadVideo(url);

    return 0;
}

void downloadVideo(const char *url) {
    char command[512];

    // Generate the command to download the video using youtube-dl
    snprintf(command, sizeof(command), "youtube-dl -o \"video.%%(ext)s\" %s", url);

    // Execute the command using system() function
    int result = system(command);

    if (result == 0) {
        printf("Download successful!\n");
    } else {
        printf("Download failed. Please check the URL and try again.\n");
    }
}

Explanation:

  1. Header Files: Include necessary header files for standard input/output and string manipulation.

  2. Macro Definition: Define constants for maximum URL length and maximum filename length.

  3. downloadVideo Function Declaration: Declare the function that will handle the video download.

  4. main Function: The main entry point of the program.

  5. Input: Prompt the user to enter the YouTube video URL and store it in the url variable.

  6. Remove Newline Character: Remove the newline character from the entered URL using strcspn.

  7. Function Call: Call the downloadVideo function with the entered URL.

  8. downloadVideo Function Definition: This function takes a YouTube video URL as a parameter and uses youtube-dl to download the video.

  9. Command Generation: Generate the command to download the video using snprintf. The -o option specifies the output filename as "video.%(ext)s".

  10. System Call: Execute the generated command using the system function.

  11. Result Handling: Check the result of the system call. If it returns 0, the download is successful; otherwise, display an error message.

  12. Print Status: Inform the user about the success or failure of the download.