# 🔐 VIDEO DQ - Cookie Authentication Setup

## Overview
Some platforms require authentication to download videos:
- ✅ **YouTube** - No auth needed
- ✅ **Twitter/X** - No auth needed  
- ❌ **Instagram** - Requires cookies
- ❌ **Facebook** - Requires cookies
- ❌ **TikTok** - Requires cookies

---

## 🚀 Setup Instructions

### Step 1: Install Firefox on Synology NAS

```bash
# Connect to your NAS via SSH
ssh user@192.168.69.105

# Install Firefox via package manager
# Option A: If you have ipkg/opkg
opkg install firefox

# Option B: Download Firefox for ARM (if Synology supports it)
wget https://download.mozilla.org/firefox/releases/latest/linux-aarch64/en-US/firefox-latest.tar.bz2
tar -xjf firefox-latest.tar.bz2 -C /volume1/
```

### Step 2: Log In to Accounts in Firefox

```bash
# Start Firefox (headless if GUI not available)
firefox &

# Or use a headless approach with your local browser:
# 1. Open Firefox on your local computer
# 2. Go to instagram.com, facebook.com, tiktok.com
# 3. Log in to each account
# 4. Keep browser open (don't clear cookies)
```

### Step 3: Verify yt-dlp Can Access Cookies

```bash
# Test if yt-dlp can read Firefox cookies
/volume1/@appstore/python314/bin/python3.14 -m yt_dlp \
  --cookies-from-browser firefox:unencrypted \
  --list-extractors | grep -E "instagram|facebook|tiktok"

# If you see extractors listed, cookies are accessible
```

### Step 4: Test with a Real Video URL

```bash
# Test Instagram
/volume1/@appstore/python314/bin/python3.14 -m yt_dlp \
  --cookies-from-browser firefox:unencrypted \
  "https://www.instagram.com/reel/YOUR_REEL_ID/" \
  -f "best" -o "test.mp4"

# Test Facebook
/volume1/@appstore/python314/bin/python3.14 -m yt_dlp \
  --cookies-from-browser firefox:unencrypted \
  "https://www.facebook.com/your_page/videos/YOUR_VIDEO_ID/" \
  -f "best" -o "test.mp4"

# Test TikTok
/volume1/@appstore/python314/bin/python3.14 -m yt_dlp \
  --cookies-from-browser firefox:unencrypted \
  "https://www.tiktok.com/@user/video/YOUR_VIDEO_ID" \
  -f "best" -o "test.mp4"
```

---

## 📋 Browser Cookie Locations

yt-dlp looks for cookies in these default locations:

### Firefox
```
Linux/Synology:
~/.mozilla/firefox/PROFILE/cookies.sqlite

Windows:
C:\Users\USERNAME\AppData\Roaming\Mozilla\Firefox\Profiles\PROFILE\cookies.sqlite

macOS:
~/Library/Application Support/Firefox/Profiles/PROFILE/cookies.sqlite
```

### Chrome/Chromium
```
Linux/Synology:
~/.config/google-chrome/Default/Cookies

Windows:
C:\Users\USERNAME\AppData\Local\Google\Chrome\User Data\Default\Cookies

macOS:
~/Library/Application Support/Google Chrome/Default/Cookies
```

---

## ⚙️ Supported Cookie Sources

The app tries to use cookies from (in order):
1. **Firefox** - Recommended, most reliable
2. **Chrome/Chromium** - If Firefox unavailable
3. **Edge** - Windows only
4. **Safari** - macOS only

---

## 🔧 Troubleshooting

### Problem: "Instagram API is not granting access"
**Solution:**
1. Make sure you're logged in to Instagram in Firefox
2. Visit instagram.com in Firefox to refresh cookies
3. Restart the app: `pm2 restart videoDQ`
4. Check if the post is viewable without login (some accounts are private)

### Problem: "Facebook requires authentication"
**Solution:**
1. Log in to Facebook in Firefox
2. Visit facebook.com to ensure cookies are saved
3. Make sure the video URL is accessible to your account

### Problem: "TikTok download fails"
**Solution:**
1. Log in to TikTok in Firefox
2. Some TikTok videos are region-restricted
3. Try downloading from the TikTok web version (not mobile)

### Problem: "File not found" after download
**Solution:**
1. Check NAS logs: `pm2 logs videoDQ --lines 50`
2. Verify the URL is correct and video exists
3. Check if the video requires payment/subscription

---

## 🛡️ Security Notes

- **Unencrypted Cookies**: The `--cookies-from-browser firefox:unencrypted` flag reads cookies without decryption (faster but requires unencrypted profile)
- **Private Data**: Cookies contain authentication tokens - keep them secure
- **Browser Sessions**: Keep Firefox logged in to maintain access
- **Cookie Expiry**: Some platforms expire cookies - re-login periodically

---

## 📊 Testing from Command Line

```bash
# Test Instagram with verbose output
/volume1/@appstore/python314/bin/python3.14 -m yt_dlp \
  --cookies-from-browser firefox:unencrypted \
  -v "https://www.instagram.com/reel/XXXX/" \
  -f "best" --write-all-thumbnails

# List available formats
/volume1/@appstore/python314/bin/python3.14 -m yt_dlp \
  --cookies-from-browser firefox:unencrypted \
  "https://www.instagram.com/reel/XXXX/" -F

# Download with subtitles (if available)
/volume1/@appstore/python314/bin/python3.14 -m yt_dlp \
  --cookies-from-browser firefox:unencrypted \
  "https://www.instagram.com/reel/XXXX/" \
  -f "best" --write-sub --write-auto-subs
```

---

## ✅ Verification Checklist

- [ ] Firefox is installed on Synology
- [ ] You're logged into Instagram/Facebook/TikTok in Firefox
- [ ] You've tested a real video URL from command line
- [ ] serverNas.js has been updated with cookie support
- [ ] videoDQ has been restarted: `pm2 restart videoDQ`
- [ ] You can see log messages indicating cookies are being used

---

## 📞 Support

If cookies aren't working:
1. Check NAS logs: `pm2 logs videoDQ --lines 100`
2. Test manually from SSH (see Testing section above)
3. Verify browser has internet access
4. Check yt-dlp version: `yt-dlp --version`
5. Update yt-dlp: `pip install yt-dlp --upgrade --break-system-packages`

---

**Last Updated:** April 2026
**App:** Video DQ v1.0+
