Teach you step by step how to use Python to connect to Qiniu Cloud interface to realize audio transcoding and cutting

WBOY
Release: 2023-07-05 16:06:55
Original
1139 people have browsed it

Teach you step by step how to use Python to connect to Qiniu Cloud interface to realize audio transcoding and cutting

Introduction:
With the development of audio technology, audio processing has also become an important link in many application scenarios. . In the audio processing process, audio transcoding and cutting are very common requirements. In order to realize these functions, we can use the interface provided by Qiniu Cloud to connect with Python to quickly and easily transcode and cut audio.

1. Environment preparation
Before starting, we need to prepare the following environment:

  1. Python development environment
  2. Install Qiniu Cloud Python SDK: through pip Install qiniu module
  3. Qiniu Cloud account: Register and log in to Qiniu Cloud account, create a new storage space for testing

2. Install Qiniu Cloud Python SDK
Execute the following command on the command line to install Qiniu Cloud Python SDK:

pip install qiniu
Copy after login

After the installation is completed, we can introduce the qiniu module into the Python code to call the Qiniu Cloud interface.

3. Audio transcoding
For audio transcoding, we can use Qiniu Cloud’s audio and video processing interface. First, we need to create a transcoding template in the Qiniu Cloud console to define audio transcoding rules. Make the following settings in the console:

  1. Enter the storage space and select the "Audio and Video" column;
  2. Click "Template" in the "Audio and Video Processing" section;
  3. Click the "New Template" button to set the audio transcoding rules according to your own needs;
  4. Save and get the template ID.

Next, we implement audio transcoding through Python code. First, we need to introduce the qiniu module and json module, the code is as follows:

import qiniu
import json
Copy after login
Copy after login

Then, we get the transcoding template ID and the URL of the audio file, the code is as follows:

access_key = 'your_access_key'
secret_key = 'your_secret_key'
bucket = 'your_bucket_name'
template_id = 'your_template_id'
audio_url = 'your_audio_url'

q = qiniu.Auth(access_key, secret_key)
base_url = 'http://api.qiniu.com'
pipeline = 'your_pipeline'
save_bucket = 'your_save_bucket_name'
save_key = 'your_save_key'

# 获得音频转码后的结果
transcode_url = "{0}/v1/handler/avthumb/query".format(base_url)
post_data = {
    "access_token": q.upload_token(bucket),
    "url": audio_url,
    "save_bucket": save_bucket,
    "save_key": save_key,
    "template_id": template_id,
    "pipeline": pipeline
}

response = qiniu.post(transcode_url, json.dumps(post_data), q.auth.authorization(transcode_url, body=json.dumps(post_data)), content_type=qiniu.conf.CONTENT_TYPE_JSON)
print(response.text_body)
Copy after login

Among them, access_key and secret_key is the key information of the Qiniu Cloud account, bucket is the name of the created storage space, template_id is the transcoding template ID, audio_url is the URL of the audio file to be transcoded, pipeline is the name of the transcoding queue, save_bucket and save_key are the transcoded audio The location where the file is saved.

4. Audio cutting
In addition to transcoding, we can also use Qiniu Cloud’s audio and video processing interface to achieve audio cutting. First, we need to select a cutting template to define the audio cutting rules. Make the following settings in the Qiniu Cloud console:

  1. Enter the storage space and select the "Audio and Video" column;
  2. Click "Template" in the "Audio and Video Processing" section;
  3. Click the "New Template" button to set the audio cutting rules according to your own needs;
  4. Save and get the template ID.

Next, we implement audio cutting through Python code. First, we need to introduce the qiniu module and json module, the code is as follows:

import qiniu
import json
Copy after login
Copy after login

Then, we get the cutting template ID and the URL of the audio file, the code is as follows:

access_key = 'your_access_key'
secret_key = 'your_secret_key'
bucket = 'your_bucket_name'
template_id = 'your_template_id'
audio_url = 'your_audio_url'
start_time = '00:00:00'
end_time = '00:01:00'
save_bucket = 'your_save_bucket_name'
save_key = 'your_save_key'

q = qiniu.Auth(access_key, secret_key)
base_url = 'http://api.qiniu.com'

# 获得音频切割后的结果
slice_url = "{0}/v1/handler/avclip/query".format(base_url)
post_data = {
    "access_token": q.upload_token(bucket),
    "url": audio_url,
    "save_bucket": save_bucket,
    "save_key": save_key,
    "template_id": template_id,
    "start": start_time,
    "end": end_time
}

response = qiniu.post(slice_url, json.dumps(post_data), q.auth.authorization(slice_url, body=json.dumps(post_data)), content_type=qiniu.conf.CONTENT_TYPE_JSON)
print(response.text_body)
Copy after login

Among them, access_key and secret_key are The key information of Qiniu Cloud account, bucket is the name of the created storage space, template_id is the cutting template ID, audio_url is the URL of the audio file to be cut, start_time is the starting time of cutting, end_time is the end time of cutting, save_bucket and save_key It is the saving location of the audio files after cutting.

Conclusion:
Through the above examples, we can see how to use Python to connect to the Qiniu Cloud interface to implement audio transcoding and cutting functions. In fact, Qiniu Cloud also provides many audio processing functions, such as audio merging, audio noise reduction, etc., which we can use according to our own needs. We hope that the introduction in this article can help readers better understand and apply audio processing technology.

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

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!