Skip to content

Instantly share code, notes, and snippets.

@argenkiwi
Created July 1, 2025 20:04
Show Gist options
  • Save argenkiwi/cd72182713cdf0e56b46f659bfb10e1e to your computer and use it in GitHub Desktop.
Save argenkiwi/cd72182713cdf0e56b46f659bfb10e1e to your computer and use it in GitHub Desktop.
Bash script to download all resources listed in an m3u file to the specified directory.
#!/bin/bash
# This script downloads files specified by URIs in an M3U playlist file.
# It requires 'wget' to be installed on your system for downloading.
# --- Configuration ---
# Default output directory if not provided by the user.
DEFAULT_OUTPUT_DIR="./downloaded_media"
# --- Function to display usage information ---
usage() {
echo "Usage: $0 -f <m3u_file_path> [-o <output_directory>]"
echo " -f <m3u_file_path> : Required. Path to the M3U playlist file."
echo " -o <output_directory> : Optional. Path to the directory where files will be downloaded."
echo " Defaults to '$DEFAULT_OUTPUT_DIR' if not provided."
exit 1
}
# --- Initialize variables ---
M3U_FILE=""
OUTPUT_DIR=""
# --- Parse command-line options ---
while getopts "f:o:" opt; do
case ${opt} in
f )
M3U_FILE=$OPTARG
;;
o )
OUTPUT_DIR=$OPTARG
;;
\? )
usage
;;
esac
done
# --- Validate required M3U file parameter ---
if [ -z "$M3U_FILE" ]; then
echo "Error: M3U file path is required."
usage
fi
# --- Set output directory default if not provided ---
if [ -z "$OUTPUT_DIR" ]; then
OUTPUT_DIR="$DEFAULT_OUTPUT_DIR"
fi
# --- Check if M3U file exists ---
if [ ! -f "$M3U_FILE" ]; then
echo "Error: M3U file not found at '$M3U_FILE'."
exit 1
fi
# --- Check if wget is installed ---
if ! command -v wget &> /dev/null; then
echo "Error: 'wget' is not installed. Please install it to use this script."
echo " On Debian/Ubuntu: sudo apt-get install wget"
echo " On macOS (with Homebrew): brew install wget"
exit 1
fi
# --- Create output directory if it doesn't exist ---
if [ ! -d "$OUTPUT_DIR" ]; then
echo "Creating output directory: '$OUTPUT_DIR'"
mkdir -p "$OUTPUT_DIR"
if [ $? -ne 0 ]; then
echo "Error: Failed to create output directory '$OUTPUT_DIR'. Check permissions."
exit 1
fi
fi
echo "--- Starting download process ---"
echo "M3U File: '$M3U_FILE'"
echo "Output Directory: '$OUTPUT_DIR'"
echo "---------------------------------"
# --- Read URIs from M3U file and download them ---
# Using 'grep -v "^#"' to skip comment lines in the M3U file.
# Using 'while IFS= read -r uri' to handle lines with spaces correctly.
grep -v "^#" "$M3U_FILE" | while IFS= read -r uri; do
# Skip empty lines
if [ -z "$uri" ]; then
continue
fi
echo "Attempting to download: '$uri'"
# Use wget to download the URI to the specified output directory.
# -P: specify directory prefix
# -nc: no clobber (don't overwrite existing files)
# -nv: no verbose (less output, just errors)
wget -P "$OUTPUT_DIR" -nc -nv "$uri"
if [ $? -eq 0 ]; then
echo " Successfully downloaded '$uri'"
else
echo " Failed to download '$uri'"
fi
echo "" # Add a newline for better readability between downloads
done
echo "--- Download process completed ---"
@argenkiwi
Copy link
Author

Thanks Gemini 2.5 Flash!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment