node.js - 使用字符串文字有效,但作为参数传递无效

标签 node.js powershell ffmpeg

我将字符串文字传递给我的 powershell 脚本,如下所示:

var spawn = require("child_process").spawn,
            child;
  child = spawn("powershell.exe", [
            "./scripts/ffmpeg_convert.ps1",
            `./cache/videos/${tempFileName + "."}${videoType}`,
            ` ./cache/converted_videos/${tempFileName + "."}${videoType}`
          ]);

spawn 是 node.js 的一部分 Spawn

还有我的 powershell 脚本 如下:
param(
    [string]$originFile = $Args[0],
    [string]$outputFile = $Args[1]
)
echo "Moving moov atom"
ffmpeg -i $originFile -vcodec copy -acodec copy -movflags +faststart $outputFile

但是,脚本不会使用参数执行
./cache/videos/${tempFileName + "."}${videoType}
但是,如果我将参数更改为文字字符串,让我们说./cache/videos/inputVideo.mov
它执行得很好。

这真的让我摸不着头脑。

我创建了两个使用相同主体的测试脚本... 就是这样 ${tempFileName + "."}${videoType}正在翻译?话虽如此,如果我回应他们的论点,它们就是预期的。

脚本 1
 ./convert_videos.ps1 ./inputVideo.mov outputVideo.mov

脚本 2
 param(
    [string]$originFile = $Args[0],
    [string]$outputFile = $Args[1]
)
echo "Moving moov atom"
ffmpeg -i $originFile -vcodec copy -acodec copy -movflags +faststart $outputFile

最佳答案

我认为您遇到了如何在 Powershell 脚本中声明参数的问题。尝试以下操作:

param(
    [Parameter(position = 0)]
    [string]$originFile,

    [Parameter(position = 1)]
    [string]$outputFile
)

# For debugging, check the values in PS
echo "Moving moov atom. originFile: $originFile. outputFile: $outputFile. Working Directory: $((Get-Location).Path)"
ffmpeg -i $originFile -vcodec copy -acodec copy -movflags +faststart $outputFile

您还可以在以更 Powershell 的方式调用脚本时命名参数,尽管这不会影响您的脚本执行,但对于我们这些沉浸在 Powershell 中的人来说,它看起来更好:)
var spawn = require("child_process").spawn,
        child;
child = spawn("powershell.exe", [
        "./scripts/ffmpeg_convert.ps1",
        "-originFile",
        `./cache/videos/${tempFileName + "."}${videoType}`,
        "-outputFile",
        ` ./cache/converted_videos/${tempFileName + "."}${videoType}`
      ]);

关于node.js - 使用字符串文字有效,但作为参数传递无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55973748/

相关文章:

powershell - 加快Powershell的remove-netfirewallrule的速度

FFMPEG - 高度不能被 2 整除

node.js - Lua从Nodejs获取参数

node.js - 无法使用 node.js 和 Mongodb 包查询 MongoDB 数据库

node.js - 当通过 Apache ProxyPass 代理时,如何在 Node.JS 上获取远程用户的 IP 地址?

wpf - PowerShell 可以用作 WPF 的代码吗

json - 将 JSON 解析为哈希表

ffmpeg添加水印libx264宽度不能被2整除(853x480)

android - FFmpeg 和样本类型

javascript - API 的快速路由 - 拥有子资源时的 URL 处理程序