Use qiniu SDK to upload videos and fragment operations.

Step1 generated token

const qiniu = require("qiniu");

var accessKey = proc.env.QINIU_ACCESS_KEY;
var secretKey = proc.env.QINIU_SECRET_KEY;
var mac = new qiniu.auth.digest.Mac(accessKey, secretKey);

var bucket = proc.env.QINIU_TEST_BUCKET;  // Object storage space

// Data processing credentials
var origin_filename = "avthumb_test_target.mp4";  // Source file name
var save_as_filename = `${origin_filename}.m3n8`;  // File name after fragmentation

var saveMp4Entry = qiniu.util.urlsafeBase64Encode(`${bucket}:${save_as_filename}`);
var avthumbMp4Fop = "avthumb/m3u8/noDomain/1/segtime/15/vb/440k|saveas/" + saveMp4Entry;

var options = {
  scope: bucket,
  expires: 3600.// The validity period of the certificate
  persistentOps: avthumbMp4Fop,  // Video uploads trigger pre-forward persistence, slice processing
  persistentPipeline: "video-pipe".// Multimedia processing queue
  persistentNotifyUrl: "http://api.example.com/qiniu/pfop/notify".// Call back the notification address
}
var putPolicy = new qiniu.rs.PutPolicy(options);
console.log(putPolicy.uploadToken(mac));
Copy the code

To generate credentials for different purposes, see the official provided nodejs-sdk example.

The above code is the certificate to generate the uploaded video and slice it. Upload related documents to view the seven Cows upload policy.

The slice action is triggered by the persistentOps parameter and starts an asynchronous task,

PersistentOps,

The persistentOps field is used to specify the storage space and resource name for the pre-processing commands and the processing results. If you specify a non-null value for this field, an asynchronous data processing task is started after a file is successfully uploaded. The persistentId field uniquely identifies this task. PersistentId when the persistentId magic variable is specified in the returnBody, the client receives the persistentId magic variable in the returnBody. PersistentId is also returned by default when a returnBody is not specified.

  • Use the default storage space and resource name * If only a data processing command is specified, the server selects the Bucket for uploading files as the storage space for data processing results. The Key is automatically generated by the Seven Ox server.
  • Use the specified storage space and resource name
    • Use pipe characters after data processing commands|Joining togethersaveas/<encodedEntryURI>Command, instructing the use of the seven cow serverEncodedEntryURIA Bucket and Key specified in the format to hold the result of the processing. Such asavthumb/flv|saveas/cWJ1Y2tldDpxa2V5To convert the uploaded video file into codeflvAfter the format isqbucket:qkey, includingcWJ1Y2tldDpxa2V5isqbucket:qkeytheUrl-safe Base64 encodingThe results. The above method can be applied to multiple data processing commands at the same time;Space, such asavthumb/mp4|saveas/cWJ1Y2tldDpxa2V5; avthumb/flv|saveas/cWJ1Y2tldDpxa2V5Mg==

Excerpt from qiniu upload voucher

Data processing commands can view official documentation, such as the video slicing interface.

Step2 Front end direct transmission

POST http://up.qiniu.comFormat: form-data Parameter: key: file name, in the previous code`origin_filename`Token: Uploads token file: indicates a specific file// The response body was uploaded successfully
// Status code 200
{
    "hash": "luVJbz9RtE3Ha7c9XaDerE6rPyvP"."key": "prTEDwvJY18YBD_720_p1.mp4"."persistentId": "Z0.5 b56c9b938b9f324a573edb4" PersistentId is available to detect the current processing status
}
Copy the code

Step3 obtain the result through polling

GET api.qiniu.com/status/get/prefop?id=${persistentId}

{
    "code": 0.// Status code 0 success, 1 waiting, 2 Processing, 3 processing failed, 4 notification submission failed.
    "desc": "The fop was completed successfully"."id": "Z0.5 b56c9b938b9f324a573edb4"."inputBucket": "ocean"."inputKey": "prTEDwvJY18YBD_720_p1.mp4"."items": [{"cmd": "avthumb/m3u8/noDomain/1/segtime/15/vb/440k|saveas/b2NlYW46aGxzX3ByVEVEd3ZKWTE4WUJEXzcyMF9wMS5tM3U4"."code": 0."desc": "The fop was completed successfully"."hash": "FraV6PX6Xp7mYYgAnY0sniTK-pPt"."key": "hls_prTEDwvJY18YBD_720_p1.m3u8"."returnOld": 0}]."pipeline": "1381326751.k12_video"."reqid": "FQIAAKAxpZfFOkQV"
}
Copy the code

The resources

Seven cows upload the certificate

Seven cows upload strategy

Video slice interface

Use NodeJS to generate credentials with data processing

nodejs-sdk example