首页 > 解决方案 > 从 AWS Lambda 层的 ffmpeg 输出中删除逗号

问题描述

我正在使用 ffmpeg Lambda 层从音频文件中获取持续时间和通道。然后我将这些详细信息输出到变量以便稍后在我的代码中使用?

任何人都可以发现/整理这段代码,因此它只输出实际值,而不是一个以逗号开头的值

在此处输入图像描述

const { spawnSync } = require("child_process");
var fs = require('fs');
const https = require('https');


exports.handler = async (event) => {
    const source_url = 'https://upload.wikimedia.org/wikipedia/commons/b/b2/Bell-ring.flac';
    const target_path = '/tmp/test.flac';

    async function downloadFile()  {
        return new Promise((resolve, reject) => {
            const file = fs.createWriteStream(target_path);
            const request = https.get(source_url, function(response) {
            response.pipe(file);
            console.log('file_downloaded!');
            resolve();
            });
        });
    }

    await downloadFile();

    const duration = spawnSync(
        "/opt/bin/ffprobe",
        [
            target_path,
            "-show_entries",
            "stream=duration", 
            "-select_streams",
            "a",
            "-of",
            "compact=p=0:nk=1",
            "-v",
            "0"
        ]
        );

        const channel = spawnSync(
        "/opt/bin/ffprobe",
        [
            target_path,
            "-show_entries",
            "stream=channels",
            "-select_streams",
            "a",
            "-of",
            "compact=p=0:nk=1",
            "-v",
            "0"
        ]
        );

    var durations = duration.output.toString('utf8');
    console.log(durations);
    var channels = channel.output.toString('utf8');
    console.log(channels);

    /*const response = {
        statusCode: 200,
        //body: JSON.stringify([channel.output.toString('utf8')])
        body: 'Complete'
    };
    return response;*/
};

只是不确定这些逗号值是从哪里来的,我需要这些值作为后面代码中比较函数的数值。

它使用这个简单的 Lambda 层,不需要外部模块

https://github.com/serverlesspub/ffmpeg-aws-lambda-layer

标签: javascriptnode.jsamazon-web-servicesffmpegaws-lambda

解决方案


推荐阅读