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:
Header Files: Include necessary header files for standard input/output and string manipulation.
Macro Definition: Define constants for maximum URL length and maximum filename length.
downloadVideo Function Declaration: Declare the function that will handle the video download.
main Function: The main entry point of the program.
Input: Prompt the user to enter the YouTube video URL and store it in the
url
variable.Remove Newline Character: Remove the newline character from the entered URL using
strcspn
.Function Call: Call the
downloadVideo
function with the entered URL.downloadVideo Function Definition: This function takes a YouTube video URL as a parameter and uses
youtube-dl
to download the video.Command Generation: Generate the command to download the video using
snprintf
. The-o
option specifies the output filename as "video.%(ext)s".System Call: Execute the generated command using the
system
function.Result Handling: Check the result of the system call. If it returns 0, the download is successful; otherwise, display an error message.
Print Status: Inform the user about the success or failure of the download.