Files
youtube-sync-docker/sync.sh
azcomputerguru 0ffb54e12e Initial commit: YouTube Sync Docker container
- Alpine-based Docker image with yt-dlp
- Configurable channel sync script
- Unraid template for Community Applications
- Automatic scheduling via cron
- Emby/Plex/Jellyfin compatible output structure
2026-05-08 18:52:04 -04:00

103 lines
3.2 KiB
Bash

#!/bin/bash
# YouTube Channel Sync Script
# Reads channel configuration from /config/channels.txt
set -e
DOWNLOAD_DIR="${DOWNLOAD_DIR:-/downloads}"
CONFIG_DIR="${CONFIG_DIR:-/config}"
COOKIES_FILE="${CONFIG_DIR}/cookies.txt"
CHANNELS_FILE="${CONFIG_DIR}/channels.txt"
MAX_QUALITY="${MAX_QUALITY:-1080}"
SLEEP_INTERVAL="${SLEEP_INTERVAL:-2}"
# Check if channels file exists
if [ ! -f "$CHANNELS_FILE" ]; then
echo "[ERROR] Channels file not found: $CHANNELS_FILE"
echo "[INFO] Creating example channels.txt file..."
cat > "$CHANNELS_FILE" << 'EOF'
# YouTube Channel Configuration
# Format: CHANNEL_ID|Channel Name
# One channel per line. Lines starting with # are ignored.
#
# Example:
# UCfDNi1aEljAQ17mUrfUjkvg|Alton Brown
# UCoq2qlWgvvKJzW_hBkLIE8w|Flavour Trip
EOF
echo "[INFO] Please edit $CHANNELS_FILE and add your channels"
exit 1
fi
# Check if cookies file exists
COOKIES_PARAM=""
if [ -f "$COOKIES_FILE" ]; then
echo "[INFO] Using cookies from: $COOKIES_FILE"
COOKIES_PARAM="--cookies $COOKIES_FILE"
else
echo "[WARNING] No cookies file found at: $COOKIES_FILE"
echo "[WARNING] Some videos may fail to download without authentication"
fi
# Parse and sync channels
echo "=========================================="
echo "YouTube Channel Sync - $(date)"
echo "=========================================="
while IFS='|' read -r CHANNEL_ID CHANNEL_NAME || [ -n "$CHANNEL_ID" ]; do
# Skip empty lines and comments
[[ -z "$CHANNEL_ID" || "$CHANNEL_ID" =~ ^[[:space:]]*# ]] && continue
# Trim whitespace
CHANNEL_ID=$(echo "$CHANNEL_ID" | xargs)
CHANNEL_NAME=$(echo "$CHANNEL_NAME" | xargs)
echo ""
echo "=========================================="
echo "Syncing: $CHANNEL_NAME"
echo "Channel ID: $CHANNEL_ID"
echo "=========================================="
yt-dlp \
$COOKIES_PARAM \
--format "bestvideo[ext=mp4][height<=${MAX_QUALITY}]+bestaudio[ext=m4a]/best[ext=mp4]/best" \
--merge-output-format mp4 \
--output "$DOWNLOAD_DIR/$CHANNEL_NAME/Season %(upload_date>%Y)s/S%(upload_date>%Y)sE%(playlist_index)03d - %(title).100B - [%(id)s].%(ext)s" \
--write-info-json \
--embed-thumbnail \
--embed-metadata \
--write-subs \
--sub-langs "en.*" \
--embed-subs \
--download-archive "$DOWNLOAD_DIR/$CHANNEL_NAME/.downloaded.txt" \
--no-overwrites \
--ignore-errors \
--sleep-interval "$SLEEP_INTERVAL" \
--max-sleep-interval 5 \
"https://www.youtube.com/channel/$CHANNEL_ID/videos"
# Create tvshow.nfo for Emby/Plex
echo ""
echo "Creating tvshow.nfo for $CHANNEL_NAME..."
cat > "$DOWNLOAD_DIR/$CHANNEL_NAME/tvshow.nfo" << EOFNFO
<?xml version='1.0' encoding='utf-8'?>
<tvshow>
<title>$CHANNEL_NAME</title>
<showtitle>$CHANNEL_NAME</showtitle>
<plot>YouTube channel: $CHANNEL_NAME</plot>
<genre>YouTube</genre>
<studio>YouTube</studio>
<premiered></premiered>
<uniqueid type="youtube" default="true">$CHANNEL_ID</uniqueid>
</tvshow>
EOFNFO
echo "Done with $CHANNEL_NAME"
echo ""
done < "$CHANNELS_FILE"
echo "=========================================="
echo "All channels synced successfully!"
echo "Completed: $(date)"
echo "=========================================="