Skip to content

Instantly share code, notes, and snippets.

@dereckmezquita
Created March 30, 2024 23:24
Show Gist options
  • Save dereckmezquita/c14acdd44e086c3ed23b025d7874987e to your computer and use it in GitHub Desktop.
Save dereckmezquita/c14acdd44e086c3ed23b025d7874987e to your computer and use it in GitHub Desktop.
Download videos in mp4 with yt-dlp and track state from links.txt
#!/usr/bin/env bash
# Input file containing links
inputFile="$1"
# Output state file
stateFile="state.csv"
# Function to handle script interruption
cleanup_and_exit() {
echo "Interrupted. Saving progress..."
# Save the current progress to the state file
> "$stateFile" # Clear the file content before writing
for url in "${!statusMap[@]}"; do
echo "$url,${statusMap["$url"]}" >> "$stateFile"
done
echo "Progress saved to $stateFile. Exiting."
exit 1
}
# Setup trap for SIGINT (Ctrl+C)
trap cleanup_and_exit SIGINT
# Check if state file exists and read it
declare -A statusMap
if [[ -f "$stateFile" ]]; then
while IFS=, read -r url status; do
statusMap["$url"]=$status
done < "$stateFile"
fi
# Function to update the state.csv file with the latest status
update_state_file() {
> "$stateFile" # Clear the file content before writing
for url in "${!statusMap[@]}"; do
echo "$url,${statusMap["$url"]}" >> "$stateFile"
done
}
# Function to download a single video
download_video() {
local url="$1"
yt-dlp -S vcodec:h264,res,acodec:m4a "$url"
return $?
}
# Main loop to process each URL
while IFS= read -r url; do
# Skip empty lines
[[ -z "$url" ]] && continue
# Check if the URL has already been successfully downloaded or not
if [[ "${statusMap["$url"]}" != "Success" ]]; then
echo "Downloading $url..."
if download_video "$url"; then
echo "Downloaded $url successfully."
statusMap["$url"]="Success"
else
echo "Failed to download $url."
statusMap["$url"]="Failed"
fi
# Update the state.csv file after each download attempt
update_state_file
else
echo "$url is already downloaded successfully."
fi
done < "$inputFile"
echo "Download process completed."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment