Package: dolbyio-rest-apis
Dolby.io REST APIs Client for PythonPython wrapper for the dolby.io RESTCommunications,StreamingandMediaAPIs.Install this projectCheck thedolbyio-rest-apispackage on PyPI. To install the latest stable python package run the following command:python3-mpipinstalldolbyio-rest-apisUpgrade your package to the latest version:python3-mpipinstall--upgradedolbyio-rest-apisLoggingYou can change the log level by using the Pythonlogginglibrary.importlogginglogging.basicConfig(level='DEBUG')AuthenticationIn order to make API calls for most operations of theCommunications APIsandMedia APIs, you must get an access token using this API:importasynciofromdolbyio_rest_apisimportauthenticationAPP_KEY='YOUR_APP_KEY'APP_SECRET='YOUR_APP_SECRET'loop=asyncio.get_event_loop()task=authentication.get_api_token(APP_KEY,APP_SECRET)at=loop.run_until_complete(task)print(f'API Token:{at.access_token}')To request a particular scope for this access token:task=authentication.get_api_token(APP_KEY,APP_SECRET,scope=['comms:*'])at=loop.run_until_complete(task)print(f'API Token:{at.access_token}')print(f'Scope:{at.scope}')Communications ExamplesGet a client access tokenTo get an access token that will be used by the client SDK for an end user to open a session against dolby.io, use the following code:importasynciofromdolbyio_rest_apisimportauthenticationasauthfromdolbyio_rest_apis.communicationsimportauthenticationAPP_KEY='YOUR_APP_KEY'APP_SECRET='YOUR_APP_SECRET'loop=asyncio.get_event_loop()# Request an API Tokentask=auth.get_api_token(APP_KEY,APP_SECRET,scope=['comms:client_access_token:create'])api_token=loop.run_until_complete(task)print(f'API Token:{api_token.access_token}')# Request the Client Access Tokentask=authentication.get_client_access_token_v2(api_token.access_token,['*'])cat=loop.run_until_complete(task)print(f'Client Access Token:{cat.access_token}')Because most of the APIs are asynchronous, you can write an async function like that:fromdolbyio_rest_apisimportauthenticationasauthfromdolbyio_rest_apis.communicationsimportauthenticationAPP_KEY='YOUR_APP_KEY'APP_SECRET='YOUR_APP_SECRET'asyncdefget_client_access_token():# Request an API Tokenapi_token=awaitauth.get_api_token(APP_KEY,APP_SECRET,scope=['comms:client_access_token:create'])# Request the Client Access Tokencat=awaitauthentication.get_client_access_token_v2(api_token.access_token,['*'])print(f'Client Access Token:{cat.access_token}')returncat.access_tokenCreate a conferenceTo create a Dolby Voice conference, you first must retrieve an API Access Token, then use the following code to create the conference.importasynciofromdolbyio_rest_apisimportauthenticationfromdolbyio_rest_apis.communicationsimportconferencefromdolbyio_rest_apis.communications.modelsimportParticipant,Permission,VideoCodecAPP_KEY='YOUR_APP_KEY'APP_SECRET='YOUR_APP_SECRET'owner_id=''# Identifier of the owner of the conferencealias=''# Conference aliasparticipants=[Participant('hostA',[Permission.JOIN,Permission.SEND_AUDIO,Permission.SEND_VIDEO],notify=True),Participant('listener1',[Permission.JOIN],notify=False),]loop=asyncio.get_event_loop()# Request an API tokentask=authentication.get_api_token(APP_KEY,APP_SECRET,scope=['comms:conf:create'])at=loop.run_until_complete(task)# Create the conferencetask=conference.create_conference(at.access_token,owner_id,alias,video_codec=VideoCodec.VP8,participants=participants)conf=loop.run_until_complete(task)print(f'Conference created:{conf.id}')Real-time Streaming ExamplesCreate a publish tokenimportasynciofromdolbyio_rest_apis.streamingimportpublish_tokenfromdolbyio_rest_apis.streaming.models.publish_tokenimportCreatePublishToken,CreateUpdatePublishTokenStreamAPI_SECRET=''# Retrieve your API Secret from the dashboardcreate_token=CreatePublishToken('my_token')create_token.streams.append(CreateUpdatePublishTokenStream('feed1',False))loop=asyncio.get_event_loop()task=publish_token.create(API_SECRET,create_token)token=loop.run_until_complete(task)print(token)Create a subscribe tokenimportasynciofromdolbyio_rest_apis.streamingimportsubscribe_tokenfromdolbyio_rest_apis.streaming.models.subscribe_tokenimportCreateSubscribeToken,CreateUpdateSubscribeTokenStreamAPI_SECRET=''# Retrieve your API Secret from the dashboardcreate_token=CreateSubscribeToken('my_token')create_token.streams.append(CreateUpdateSubscribeTokenStream('feed1',False))loop=asyncio.get_event_loop()task=publish_token.create(API_SECRET,create_token)token=loop.run_until_complete(task)print(token)Media ExamplesHere is an example on how to upload a file to the Dolby.io temporary cloud storage, enhance that file and download the result.Get an API tokenGet the App Key and Secret from the Dolby.io dashboard and use the following code in your python script.importasynciofromdolbyio_rest_apisimportauthenticationAPP_KEY='YOUR_APP_KEY'APP_SECRET='YOUR_APP_SECRET'loop=asyncio.get_event_loop()task=authentication.get_api_token(APP_KEY,APP_SECRET,2*60*60)# 2 hoursat=loop.run_until_complete(task)print(f'API token:{at.access_token}')Upload a file for processingAdd the followingimportto your script.fromdolbyio_rest_apis.mediaimportioUsing the API token, start by requesting an upload URL.# Temporary storage URL that will be used as reference for the job processinginput_url='dlb://in/file.mp4'# Get an Upload URLtask=io.get_upload_url(access_token=at.access_token,dlb_url=input_url,)upload_url=loop.run_until_complete(task)print(f'Upload URL:{upload_url}')Upload a media file to the Dolby.io temporary cloud storage for processing:# Location of the original file on the local machineIN_FILE_PATH='/path/to/original_file.mp4'# Upload the filetask=io.upload_file(upload_url=upload_url,file_path=IN_FILE_PATH,)loop.run_until_complete(task)Start an enhance jobAdd the followingimportto your script.importjsonfromdolbyio_rest_apis.mediaimportenhanceGenerate a job description and send it to Dolby.io.# Temporary storage URL where the service will write the result file tooutput_url='dlb://out/file.mp4'# Job descriptionjob={'input':input_url,'output':output_url,'content':{'type':'podcast',},}job_str=json.dumps(job,indent=4)print(job_str)# Start the enhance job and get a job ID backtask=enhance.start(at.access_token,job_str)job_id=loop.run_until_complete(task)print(f'Job ID:{job_id}')Wait for the job to completeAdd the followingimportto your script.importsysimporttimeGet the job status and wait until it is completed.task=enhance.get_results(at.access_token,job_id)result=loop.run_until_complete(task)whileresult.statusin('Pending','Running'):print(f'Job status is{result.status}, taking a 5 second break...')time.sleep(5)task=enhance.get_results(at.access_token,job_id)result=loop.run_until_complete(task)ifresult.status!='Success':print('There was a problem with processing the file')print(json.dumps(result,indent=4))sys.exit(1)Download a processed fileAt this stage, the file has been processed and written to the temporary storage so we can download it.# Where to download the file on the local machineOUT_FILE_PATH='/path/to/processed_file.mp4'task=io.download_file(access_token=at.access_token,dlb_url=output_url,file_path=OUT_FILE_PATH,)loop.run_until_complete(task)