Table of Contents
Preparation work
Connect to Huawei Cloud
Create a Huawei Cloud connection instance
Upload audio files
Audio transcoding
View the transcoding task status
Download the transcoded audio file
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

Jul 05, 2023 pm 04:13 PM
python Huawei Cloud audio transcoding

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:

1

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:

1

import obs

Copy after login

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

1

2

3

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:

1

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:

1

2

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:

1

2

3

4

5

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:

1

2

3

4

5

6

7

8

9

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:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

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:

1

2

3

4

5

6

7

8

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:

1

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:

1

2

3

4

5

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!

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the advantages and disadvantages of templating? What are the advantages and disadvantages of templating? May 08, 2024 pm 03:51 PM

What are the advantages and disadvantages of templating?

How to download deepseek Xiaomi How to download deepseek Xiaomi Feb 19, 2025 pm 05:27 PM

How to download deepseek Xiaomi

Google AI announces Gemini 1.5 Pro and Gemma 2 for developers Google AI announces Gemini 1.5 Pro and Gemma 2 for developers Jul 01, 2024 am 07:22 AM

Google AI announces Gemini 1.5 Pro and Gemma 2 for developers

How do you ask him deepseek How do you ask him deepseek Feb 19, 2025 pm 04:42 PM

How do you ask him deepseek

How to save the evaluate function How to save the evaluate function May 07, 2024 am 01:09 AM

How to save the evaluate function

How to search deepseek How to search deepseek Feb 19, 2025 pm 05:18 PM

How to search deepseek

What software is NET40? What software is NET40? May 10, 2024 am 01:12 AM

What software is NET40?

What language is the browser plug-in written in? What language is the browser plug-in written in? May 08, 2024 pm 09:36 PM

What language is the browser plug-in written in?

See all articles