11##Usage
22
3- ** Usage by transcoding profile ID **
3+ ** Sample Code **
44
55````
6- import sys
7- import os.path
8- sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir)))
9- import qencode
10- import time
11-
12-
13- API_KEY = 'Your API KEY'
14- TRANSCODING_PROFILEID = 'Your profile ID'
15- VIDO_URL = 'your source url'
16-
17-
18-
19- def start_encode():
20- """
21- Create client object
22- :param api_key: string. required
23- :param api_url: string. not required
24- :param api_version: int. not required. default 'v1'
25- :return: client object
26- """
27- client = qencode.client(API_KEY)
28- client.create()
29- if client.error:
30- print 'encoder error:', client.error, client.message
31- raise SystemExit
32-
33- """
34- :return: task object
35- """
36- task = client.create_task()
37- task.start_time = 0.0
38- task.duration = 10.0
39- task.start(TRANSCODING_PROFILEID, VIDO_URL)
40- if task.error:
41- print 'task error:', task.error, task.message
42- raise SystemExit
43-
44- while True:
45- status = task.status()
46- print '{0} | {1} | {2} | error: {3}'.format(VIDO_URL,
47- status.get('status'),
48- status.get('percent'),
49- status.get('error'),
50- status.get('error_description'))
51- if status['error']:
52- break
53- if status['status'] == 'completed':
54- break
55- time.sleep(15)
56-
6+ #!/usr/bin/python
7+ # -*- coding: utf-8 -*-
578
58- if __name__ == '__main__':
59- start_encode()
60- ````
61-
62- ** Usage by custom parameters**
63-
64- ````
659import sys
6610import os.path
6711sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir)))
6812import qencode
6913import time
70-
71- API_KEY = 'Your API KEY'
72-
73- params = qencode.custom_params()
74-
75- FORMAT = qencode.format()
76- STREAM = qencode.stream()
77- DESTINATION = qencode.destination()
78- VIDEO_CODEC = qencode.x264_video_codec()
79-
80-
81- DESTINATION.url = "..."
82- DESTINATION.key = "..."
83- DESTINATION.secret = "..."
84-
85- VIDEO_CODEC.vprofile = "baseline"
86- VIDEO_CODEC.level = 31
87- VIDEO_CODEC.coder = 0
88- VIDEO_CODEC.flags2 = "-bpyramid+fastpskip-dct8x8"
89- VIDEO_CODEC.partitions = "+parti8x8+parti4x4+partp8x8+partb8x8"
90- VIDEO_CODEC.directpred = 2
91-
92- STREAM.profile = "baseline"
93- STREAM.size = "1920x1080"
94- STREAM.audio_bitrate = 128
95- STREAM.video_codec_parameters = VIDEO_CODEC
96-
97- FORMAT.stream = [STREAM]
98- FORMAT.output = "advanced_hls"
99- FORMAT.destination = DESTINATION
100-
101- params.source = 'your source url'
102- params.format = [FORMAT]
14+ import json
15+ from qencode import QencodeClientException, QencodeTaskException
16+
17+ #replace with your API KEY (can be found in your Project settings on Qencode portal)
18+ API_KEY = 'your-api-qencode-key'
19+
20+ params = """
21+ {"query": {
22+ "source": "https://qencode.com/static/1.mp4",
23+ "format": [
24+ {
25+ "output": "mp4",
26+ "size": "320x240",
27+ "video_codec": "libx264"
28+ }
29+ ]
30+ }
31+ }
32+ """
10333
10434
10535def start_encode():
@@ -109,59 +39,38 @@ def start_encode():
10939 :param api_key: string. required
11040 :param api_url: string. not required
11141 :param api_version: int. not required. default 'v1'
112- :return: client object
42+ :return: task object
11343 """
44+
11445 client = qencode.client(API_KEY)
115- client.create()
11646 if client.error:
117- print 'encoder error:', client.error, client.message
118- raise SystemExit
47+ raise QencodeClientException(client.message)
11948
120- """
121- Create task
122- :return: task object
123- """
49+ print 'The client created. Expire date: %s' % client.expire
12450
12551 task = client.create_task()
52+
53+ if task.error:
54+ raise QencodeTaskException(task.message)
55+
12656 task.custom_start(params)
57+
12758 if task.error:
128- print 'task error:', task.error, task.message
129- raise SystemExit
59+ raise QencodeTaskException(task.message)
60+
61+ print 'Start encode. Task: %s' % task.task_token
13062
13163 while True:
13264 status = task.status()
133- print '{0} | {1} | {2} | error: {3}'.format(params.source,
134- status.get('status'),
135- status.get('percent'),
136- status.get('error'),
137- status.get('error_description'))
138- if status['error']:
65+ # print status
66+ print json.dumps(status, indent=2, sort_keys=True)
67+ if status['error'] or status['status'] == 'completed':
13968 break
140- if status['status'] == 'completed':
141- break
142- time.sleep(15)
143-
69+ time.sleep(5)
14470
14571if __name__ == '__main__':
14672 start_encode()
147- ````
148- ** Usage with callback methods**
149-
150- ````
151- def my_callback(e):
152- print e
153-
154- def my_callback2(e):
155- print e
156-
157- ...
158-
159- task.start(TRANSCODING_PROFILEID, VIDO_URL)
160- if task.error:
161- raise SystemExit
16273
163- task.progress_changed(my_callback)
164- task.task_completed(my_callback2)
16574````
16675
16776** Documentation**
0 commit comments