-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquickstart.py
More file actions
279 lines (227 loc) · 9.12 KB
/
Copy pathquickstart.py
File metadata and controls
279 lines (227 loc) · 9.12 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#!/usr/bin/env python3
"""
GhostStream Quick Start Examples
================================
Copy-paste examples to get started in 30 seconds.
No GhostHub required - works with any video source.
Run GhostStream first:
python -m ghoststream
Then run these examples:
python examples/quickstart.py
"""
import time
from ghoststream import GhostStreamClient, TranscodeStatus
# Change this to your GhostStream server
GHOSTSTREAM_SERVER = "localhost:8765"
# Create a shared client instance
client = GhostStreamClient(manual_server=GHOSTSTREAM_SERVER)
# =============================================================================
# EXAMPLE 1: Transcode a URL to HLS stream
# =============================================================================
def example_url_to_hls():
"""
Transcode any video URL to HLS for web playback.
Works with: HTTP URLs, RTSP streams, S3 URLs, etc.
"""
print("\n🎬 Example 1: URL to HLS Stream")
print("-" * 40)
# Any video URL works - HTTP, RTSP, S3, etc.
video_url = "https://test-videos.co.uk/vids/bigbuckbunny/mp4/h264/1080/Big_Buck_Bunny_1080_10s_1MB.mp4"
# Start transcoding using SDK
job = client.transcode(
source=video_url,
mode="stream",
resolution="720p",
video_codec="h264"
)
if job.status == TranscodeStatus.ERROR:
print(f"❌ Error: {job.error_message}")
return
print(f"✅ Job started: {job.job_id}")
# Wait for stream to be ready
print(" Waiting for transcode...")
result = client.wait_for_ready(job.job_id, timeout=30)
if result and result.status == TranscodeStatus.READY:
print(f"✅ Stream ready!")
print(f" URL: {result.stream_url}")
print(f"\n Play with VLC: vlc {result.stream_url}")
print(f" Or in browser with hls.js")
elif result and result.status == TranscodeStatus.ERROR:
print(f"❌ Error: {result.error_message}")
elif result and result.stream_url:
print(f"✅ Stream available (still processing)!")
print(f" URL: {result.stream_url}")
# Cleanup
input("\nPress Enter to cleanup...")
client.delete_job(job.job_id)
print("✅ Cleaned up")
# =============================================================================
# EXAMPLE 2: Transcode local file (via file server)
# =============================================================================
def example_local_file():
"""
Transcode a local file by serving it over HTTP.
Option A: Use Python's built-in server:
cd /path/to/videos && python -m http.server 8000
Option B: Use any web server (nginx, Apache, etc.)
"""
print("\n🎬 Example 2: Local File via HTTP")
print("-" * 40)
print("""
To transcode local files, serve them over HTTP first:
1. Open a new terminal
2. cd to your video folder
3. Run: python -m http.server 8000
4. Your video is now at: http://localhost:8000/video.mp4
Then use that URL with GhostStream.
""")
# =============================================================================
# EXAMPLE 3: Adaptive Bitrate (multiple qualities)
# =============================================================================
def example_adaptive_bitrate():
"""
Create Netflix-style adaptive streaming with multiple quality levels.
Player automatically switches quality based on bandwidth.
"""
print("\n🎬 Example 3: Adaptive Bitrate (ABR)")
print("-" * 40)
video_url = "https://test-videos.co.uk/vids/bigbuckbunny/mp4/h264/1080/Big_Buck_Bunny_1080_10s_1MB.mp4"
# Start ABR transcoding using SDK
job = client.transcode(
source=video_url,
mode="abr", # Adaptive bitrate - creates 1080p, 720p, 480p variants
video_codec="h264"
)
if job.status == TranscodeStatus.ERROR:
print(f"❌ Error: {job.error_message}")
return
print(f"✅ ABR job started: {job.job_id}")
print(" Creating quality variants: 1080p, 720p, 480p...")
# Wait for ready
result = client.wait_for_ready(job.job_id, timeout=60)
if result and result.status == TranscodeStatus.READY:
print(f"✅ ABR stream ready!")
print(f" Master playlist: {result.stream_url}")
print(f"\n The master.m3u8 contains all quality variants.")
print(f" HLS players (VLC, hls.js) auto-select best quality.")
elif result and result.status == TranscodeStatus.ERROR:
print(f"❌ Error: {result.error_message}")
elif result and result.stream_url:
print(f"✅ ABR stream available!")
print(f" Master playlist: {result.stream_url}")
input("\nPress Enter to cleanup...")
client.delete_job(job.job_id)
# =============================================================================
# EXAMPLE 4: Check hardware capabilities
# =============================================================================
def example_check_hardware():
"""
See what hardware acceleration is available.
"""
print("\n🎬 Example 4: Hardware Capabilities")
print("-" * 40)
# Get capabilities using SDK
caps = client.get_capabilities()
if not caps:
print("❌ Error: Could not get capabilities")
return
print(f"FFmpeg: {caps.get('ffmpeg_version', 'unknown')}")
print(f"Platform: {caps.get('platform', 'unknown')}")
print(f"\nHardware Acceleration:")
for hw in caps.get("hw_accels", []):
status = "✅" if hw.get("available") else "❌"
print(f" {status} {hw.get('type', 'unknown').upper()}")
if hw.get("available") and hw.get("encoders"):
print(f" Encoders: {', '.join(hw['encoders'][:3])}")
print(f"\nVideo Codecs: {', '.join(caps.get('video_codecs', []))}")
print(f"Audio Codecs: {', '.join(caps.get('audio_codecs', []))}")
# =============================================================================
# EXAMPLE 5: Simple health check
# =============================================================================
def example_health_check():
"""
Check if GhostStream is running and healthy.
"""
print("\n🎬 Example 5: Health Check")
print("-" * 40)
# Check health using SDK
if client.health_check():
print(f"✅ GhostStream is healthy")
# Get more details from capabilities
caps = client.get_capabilities()
if caps:
print(f" Version: {caps.get('version', 'unknown')}")
print(f" Platform: {caps.get('platform', 'unknown')}")
else:
print(f"❌ Cannot connect to GhostStream at {GHOSTSTREAM_SERVER}")
print(f" Make sure GhostStream is running: python -m ghoststream")
# =============================================================================
# EXAMPLE 6: Start from specific time (seeking)
# =============================================================================
def example_seeking():
"""
Start transcoding from a specific timestamp.
Useful for resume playback.
"""
print("\n🎬 Example 6: Seek to Timestamp")
print("-" * 40)
video_url = "https://test-videos.co.uk/vids/bigbuckbunny/mp4/h264/1080/Big_Buck_Bunny_1080_10s_1MB.mp4"
# Start transcoding from specific timestamp using SDK
job = client.transcode(
source=video_url,
mode="stream",
start_time=5, # Start from 5 seconds
resolution="720p"
)
if job.status != TranscodeStatus.ERROR:
print(f"✅ Started from 5 seconds: {job.job_id}")
# Cleanup after a moment
time.sleep(3)
client.delete_job(job.job_id)
print("✅ Cleaned up")
else:
print(f"❌ Error: {job.error_message}")
# =============================================================================
# MAIN
# =============================================================================
if __name__ == "__main__":
print("=" * 50)
print("GhostStream Quick Start Examples")
print("=" * 50)
print(f"\nServer: {GHOSTSTREAM_SERVER}")
print("\nMake sure GhostStream is running first:")
print(" python -m ghoststream")
print("\n" + "=" * 50)
# Check server first
example_health_check()
print("\n" + "=" * 50)
print("Select an example to run:")
print(" 1. URL to HLS Stream")
print(" 2. Local File (instructions)")
print(" 3. Adaptive Bitrate (ABR)")
print(" 4. Hardware Capabilities")
print(" 5. Health Check")
print(" 6. Seek to Timestamp")
print(" 0. Run all")
print("=" * 50)
choice = input("\nEnter choice (1-6, or 0 for all): ").strip()
if choice == "1":
example_url_to_hls()
elif choice == "2":
example_local_file()
elif choice == "3":
example_adaptive_bitrate()
elif choice == "4":
example_check_hardware()
elif choice == "5":
example_health_check()
elif choice == "6":
example_seeking()
elif choice == "0":
example_health_check()
example_check_hardware()
example_url_to_hls()
example_adaptive_bitrate()
example_seeking()
else:
print("Invalid choice")