Home > Backend Development > Python Tutorial > Teach you how to use Python to connect to Huawei Cloud interface to achieve audio transcoding and storage

Teach you how to use Python to connect to Huawei Cloud interface to achieve audio transcoding and storage

WBOY
Release: 2023-07-05 16:13:28
Original
1231 people have browsed it

Teach you how to use Python to connect to the Huawei Cloud interface to achieve audio transcoding and storage

Huawei Cloud is a cloud computing service platform launched by Huawei, aiming to provide users with stable, secure, and high-performance Cloud computing services. On Huawei Cloud Platform, we can implement various functions through API interfaces, such as audio transcoding and storage. This article will introduce how to use Python to connect to the Huawei Cloud interface to implement audio transcoding and storage functions.

Preparation work

First, we need to install the Huawei Cloud SDK for Python, which can be installed through the pip command:

pip install obs-sdk
Copy after login

In addition, we also need Create a Huawei Cloud account and create a bucket on the console to store the transcoded audio files.

Connect to Huawei Cloud

First, introduce the relevant modules of Huawei Cloud SDK in Python:

import obs
Copy after login

Then, we need to set the security credentials of the account (Access Key and Secret Key ):

access_key = 'your_access_key'
secret_key = 'your_secret_key'
obs_endpoint = 'https://obs.cn-north-1.myhwclouds.com'
Copy after login

Create a Huawei Cloud connection instance

Next, we need to create a Huawei Cloud connection instance:

obs_client = obs.ObsClient(access_key, secret_key, obs_endpoint)
Copy after login

Upload audio files

Now, we can upload the audio file to the Huawei Cloud storage bucket. First, we need to specify the local file path and target path to be uploaded:

local_file_path = 'your_local_file_path'
dest_file_path = 'your_dest_file_path'
Copy after login

Then, the audio file can be uploaded to Huawei Cloud through the following code:

resp = obs_client.putFile('your_bucket_name', dest_file_path, local_file_path)
if resp.status >= 300:
    print('音频文件上传失败')
else:
    print('音频文件上传成功')
Copy after login

Audio transcoding

Audio transcoding is the process of converting an audio file to a different format or encoding. On the Huawei Cloud platform, we can use the audio transcoding service to transcode audio files into different formats. First, we need to configure the transcoding parameters:

transcoding_job_name = 'your_transcoding_job_name'
transcoding_input_bucket = 'your_input_bucket_name'
transcoding_output_bucket = 'your_output_bucket_name'
transcoding_input_key = dest_file_path
transcoding_output_key = 'your_output_file_path'
transcoding_output_format = 'your_output_format'
transcoding_output_sample_rate = 'your_output_sample_rate'
transcoding_output_bit_rate = 'your_output_bit_rate'
transcoding_output_channels = 'your_output_channels'
Copy after login

Then, you can submit the transcoding task through the following code:

input_param = {
    'object': transcoding_input_key
}
output_param = {
    'object': transcoding_output_key,
    'format': transcoding_output_format,
    'sample_rate': transcoding_output_sample_rate,
    'bit_rate': transcoding_output_bit_rate,
    'channels': transcoding_output_channels
}
resp = obs_client.createMediaJob(transcoding_job_name, transcoding_input_bucket, transcoding_output_bucket, input_param, output_param)
if resp.status >= 300:
    print('音频转码任务提交失败')
else:
    print('音频转码任务提交成功')
Copy after login

View the transcoding task status

You can use the following code Check the status of the transcoding task:

resp = obs_client.queryJobStatus(transcoding_job_name)
if resp.status >= 300:
    print('查询转码任务状态失败')
else:
    status = resp.body['Status']
    progress = resp.body['Progress']
    print(f'转码任务状态:{status}')
    print(f'转码任务进度:{progress}')
Copy after login

Download the transcoded audio file

Finally, we can download the transcoded audio file to the local. First, specify the storage path of the file to be downloaded:

download_file_path = 'your_local_download_file_path'
Copy after login

Then, the transcoded audio file can be downloaded to the local through the following code:

resp = obs_client.getFile('your_output_bucket_name', transcoding_output_key, download_file_path)
if resp.status >= 300:
    print('音频文件下载失败')
else:
    print('音频文件下载成功')
Copy after login

At this point, we are done Use Python to connect to the Huawei Cloud interface to implement audio transcoding and storage functions. Through the introduction of this article, I hope to help readers make better use of Huawei Cloud Platform and realize the functions they want.

The above is the detailed content of Teach you how to use Python to connect to Huawei Cloud interface to achieve audio transcoding and storage. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template