File size: 4,215 Bytes
d1290e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# YouTube Live Streamer - API Guide

## Architecture

```
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Vercel        β”‚ ──▢  β”‚  Hugging Face    β”‚ ──▢  β”‚    YouTube      β”‚
β”‚  (Dashboard)    β”‚ API  β”‚  (Streaming API) β”‚ RTMP β”‚  (Live Stream)  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                              β”‚
                              β–Ό
                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                    β”‚   Google Drive   β”‚
                    β”‚   (Videos)       β”‚
                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```

## API Endpoints

### 1. Get Status
```
GET /api/status
```
**Response:**
```json
{
  "is_running": true,
  "status": "streaming",
  "current_video": {
    "name": "video1.mp4",
    "id": "drive_file_id",
    "started_at": "2026-05-07T10:30:00"
  },
  "videos_queue": [
    {"id": "id1", "name": "video1.mp4"},
    {"id": "id2", "name": "video2.mp4"}
  ],
  "videos_played": 5,
  "session_start": "2026-05-07T10:00:00",
  "session_end": "2026-05-07T16:00:00",
  "total_stream_time": 18000,
  "remaining_time_minutes": 120
}
```

### 2. Start Stream
```
POST /api/start
```
**Body:**
```json
{
  "duration_hours": 6.5,
  "shuffle": true,
  "youtube_stream_key": "xxxx-xxxx-xxxx"  // optional, can use env var
}
```
**Response:**
```json
{
  "success": true,
  "message": "Stream started for 6.5 hours",
  "duration_hours": 6.5,
  "shuffle": true
}
```

### 3. Stop Stream
```
POST /api/stop
```
**Response:**
```json
{
  "success": true,
  "message": "Stop signal sent. Stream will end after current video."
}
```

### 4. Get Available Videos
```
GET /api/videos
```
**Response:**
```json
{
  "count": 10,
  "videos": [
    {"id": "drive_id_1", "name": "video1.mp4"},
    {"id": "drive_id_2", "name": "video2.mp4"}
  ]
}
```

## Environment Variables

| Variable | Required | Default | Description |
|----------|----------|---------|-------------|
| `YOUTUBE_STREAM_KEY` | Yes | - | YouTube stream key |
| `GOOGLE_OAUTH_CLIENT_ID` | Yes | - | Google OAuth client ID |
| `GOOGLE_OAUTH_CLIENT_SECRET` | Yes | - | Google OAuth client secret |
| `GOOGLE_DRIVE_REFRESH_TOKEN` | Yes | - | Drive refresh token |
| `GOOGLE_DRIVE_FOLDER_ID` | Yes | - | Drive folder with videos |
| `API_MODE` | No | `true` | Run as API server |
| `SESSION_DURATION_HOURS` | No | `6` | Default stream duration |
| `SHUFFLE_VIDEOS` | No | `true` | Shuffle video order |

## Frontend Example (React/Next.js)

```typescript
const API_URL = process.env.NEXT_PUBLIC_STREAMER_API_URL;

// Get status
const status = await fetch(`${API_URL}/api/status`).then(r => r.json());

// Start stream
await fetch(`${API_URL}/api/start`, {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({ duration_hours: 6, shuffle: true })
});

// Stop stream
await fetch(`${API_URL}/api/stop`, { method: 'POST' });
```

## Running Locally

```bash
# Install dependencies
pip install -r requirements.txt

# Set environment variables
export YOUTUBE_STREAM_KEY=xxxx
export GOOGLE_OAUTH_CLIENT_ID=xxxx
export GOOGLE_OAUTH_CLIENT_SECRET=xxxx
export GOOGLE_DRIVE_REFRESH_TOKEN=xxxx
export GOOGLE_DRIVE_FOLDER_ID=xxxx

# Run API server
python app.py

# Or with explicit env
API_MODE=true python app.py
```

## Deployment

### Hugging Face Space
1. Create new Space with Docker SDK
2. Upload this code
3. Set environment variables in Space Settings
4. Space will auto-run API on port 7860

### Vercel Frontend
1. Create Next.js app
2. Call HF API endpoints
3. Show live status, current video, queue
4. Add start/stop controls

## Important Notes

- Stream stops gracefully after current video when `stop` is called
- Videos are downloaded to `/tmp` and cleaned up after streaming
- Each session shuffles videos (if enabled) for variety
- Session auto-stops after `duration_hours` even if videos remain