Skip to content

Commit 4974fd3

Browse files
committed
status printing for sample code
1 parent d78c56f commit 4974fd3

File tree

4 files changed

+75
-35
lines changed

4 files changed

+75
-35
lines changed

qencode/custom_params.py

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -136,33 +136,25 @@ def validate_params(self):
136136
self.error = True
137137
self.message = 'for custom start encode - params is required'
138138
return
139-
if not self.params.source:
139+
if not 'source' in self.params.__dict__:
140140
self.error = True
141141
self.message = 'Params: source is required'
142142
return
143-
if not self.params.format:
143+
if not 'format' in self.params.__dict__:
144144
self.error = True
145145
self.message = 'Params: format is required'
146146
return
147147
for format in self.params.format:
148-
if not format.stream:
148+
if not 'stream' in format.__dict__:
149149
self.error = True
150150
self.message = 'Params: stream is required in the format list'
151151
return
152-
if not format.output:
152+
if not 'output' in format.__dict__:
153153
self.error = True
154154
self.message = 'Params: output format is required in the format list'
155155
return
156-
if not format.destination:
157-
self.error = True
158-
self.message = 'Params: destination is required in the format list'
159-
return
160156
for stream in format.stream:
161-
if not stream.video_codec_parameters:
162-
self.error = True
163-
self.message = 'Params: video_codec_parameters is required in the stream list'
164-
return
165-
if not stream.size:
157+
if not 'size' in stream.__dict__:
166158
self.error = True
167159
self.message = 'Params: size is required in the stream list'
168160
return

sample-code/start_encode.py

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir)))
77
import qencode
88
import time
9+
import json
910

1011

1112
API_KEY = '5a5db6fa5b4c5'
1213
TRANSCODING_PROFILEID = '5a5db6fa5b8ac'
1314
VIDO_URL = 'https://qa.qencode.com/static/1.mp4'
15+
line = "-" * 80 + "\n"
1416

1517

1618

@@ -22,9 +24,10 @@ def start_encode():
2224
:param api_version: int. not required. default 'v1'
2325
:return: client object
2426
"""
27+
print line
2528
client = qencode.client(API_KEY)
2629
if client.error:
27-
print 'encoder error:', client.error, client.message
30+
print 'Error: %s\n' % client.message
2831
raise SystemExit
2932

3033
"""
@@ -35,22 +38,35 @@ def start_encode():
3538
task.duration = 10.0
3639
task.start(TRANSCODING_PROFILEID, VIDO_URL)
3740
if task.error:
38-
print 'task error:', task.error, task.message
41+
print 'Error: %s\n' % task.message
3942
raise SystemExit
4043

4144
while True:
4245
status = task.status()
43-
print '{0} | {1} | {2} | error: {3}'.format(VIDO_URL,
44-
status.get('status'),
45-
status.get('percent'),
46-
status.get('error'),
47-
status.get('error_description'))
46+
try:
47+
print_status(status)
48+
except BaseException as e:
49+
print str(e)
4850
if status['error']:
4951
break
5052
if status['status'] == 'completed':
5153
break
52-
time.sleep(15)
54+
time.sleep(10)
55+
56+
57+
def print_status(status):
58+
if not status['error'] and status['status'] != 'error':
59+
print "Status: {0} {1}%".format(status.get('status'), status.get('percent'))
60+
elif status['error'] or status['status'] == 'error':
61+
print "Error: %s\n" % (status.get('error_description'))
62+
if status['status'] == 'completed':
63+
print line
64+
for video in status['videos']:
65+
meta = json.loads(video['meta'])
66+
print 'Resolution: %s' % meta.get('resolution')
67+
print 'Url: %s' % video.get('url')
68+
print line
5369

5470

5571
if __name__ == '__main__':
56-
start_encode()
72+
start_encode()

sample-code/start_encode2.py

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir)))
77
import qencode
88
import time
9+
import json
910

1011
API_KEY = '5a5db6fa5b4c5'
1112

@@ -40,6 +41,7 @@
4041
params.source = 'https://qa.qencode.com/static/1.mp4'
4142
params.format = [FORMAT]
4243

44+
line = "-" * 80 + "\n"
4345

4446
def start_encode():
4547

@@ -50,9 +52,11 @@ def start_encode():
5052
:param api_version: int. not required. default 'v1'
5153
:return: client object
5254
"""
55+
56+
print line
5357
client = qencode.client(API_KEY)
5458
if client.error:
55-
print 'encoder error:', client.error, client.message
59+
print 'Error: %s\n' % client.message
5660
raise SystemExit
5761

5862
"""
@@ -63,22 +67,33 @@ def start_encode():
6367
task = client.create_task()
6468
task.custom_start(params)
6569
if task.error:
66-
print 'task error:', task.error, task.message
70+
print 'Error: %s' % task.message
6771
raise SystemExit
6872

6973
while True:
7074
status = task.status()
71-
print '{0} | {1} | {2} | error: {3}'.format(params.source,
72-
status.get('status'),
73-
status.get('percent'),
74-
status.get('error'),
75-
status.get('error_description'))
75+
try:
76+
print_status(status)
77+
except BaseException as e:
78+
print str(e)
7679
if status['error']:
7780
break
7881
if status['status'] == 'completed':
7982
break
80-
time.sleep(15)
81-
83+
time.sleep(10)
84+
85+
def print_status(status):
86+
if not status['error'] and status['status'] != 'error':
87+
print "Status: {0} {1}%".format(status.get('status'), status.get('percent'))
88+
elif status['error'] or status['status'] == 'error':
89+
print "Error: %s\n" % (status.get('error_description'))
90+
if status['status'] == 'completed':
91+
print line
92+
for video in status['videos']:
93+
meta = json.loads(video['meta'])
94+
print 'Resolution: %s' % meta.get('resolution')
95+
print 'Url: %s' % video.get('url')
96+
print line
8297

8398
if __name__ == '__main__':
8499
start_encode()

sample-code/start_encode3.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,34 @@
66
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir)))
77
import qencode
88
import time
9+
import json
910

1011

1112

1213
API_KEY = '5a5db6fa5b4c5'
1314
TRANSCODING_PROFILEID = '5a5db6fa5b8ac'
1415
VIDO_URL = 'https://qa.qencode.com/static/1.mp4'
1516

17+
line = "-" * 80 + "\n"
1618

1719
def my_callback(e):
18-
print e
20+
print_status(e)
1921

2022
def my_callback2(e):
21-
print e
23+
print_status(e)
24+
25+
def print_status(status):
26+
if not status['error'] and status['status'] != 'error':
27+
print "Status: {0} {1}%".format(status.get('status'), status.get('percent'))
28+
elif status['error'] or status['status'] == 'error':
29+
print "Error: %s\n" % (status.get('error_description'))
30+
if status['status'] == 'completed':
31+
print line
32+
for video in status['videos']:
33+
meta = json.loads(video['meta'])
34+
print 'Resolution: %s' % meta.get('resolution')
35+
print 'Url: %s' % video.get('url')
36+
print line
2237

2338
def start_encode():
2439
"""
@@ -29,7 +44,7 @@ def start_encode():
2944
"""
3045
client = qencode.client(API_KEY)
3146
if client.error:
32-
print 'encoder error:', client.error, client.message
47+
print 'Error: %s\n' % client.message
3348
raise SystemExit
3449

3550
"""
@@ -41,12 +56,14 @@ def start_encode():
4156
task = client.create_task()
4257
task.start(TRANSCODING_PROFILEID, VIDO_URL)
4358
if task.error:
44-
print 'task error:', task.error, task.message
59+
print 'Error: %s\n' % task.message
4560
raise SystemExit
4661

4762
task.progress_changed(my_callback)
4863
task.task_completed(my_callback2)
4964

5065

66+
67+
5168
if __name__ == '__main__':
5269
start_encode()

0 commit comments

Comments
 (0)