c - 如何在 Shell FFmpeg 中转义单引号?

标签 c bash shell ffmpeg zsh

我的环境

  • zsh
  • 苹果 clang 版本 13.0.0

  • 我的问题总结
    我想使用 FFmpeg 在视频上绘制一些文本。
    我的命令行是由带有 system() 函数的 C 程序发送的。
    当我的字符串中有单引号时,文本不会显示,这是有道理的。
    我试过的
  • 保持原样 → 不绘制文本
  • 用\' 正常转义它 → 不绘制文本
  • 使用\\' 双重转义 → 不绘制文本
  • 三重逃避它,等等...
  • 使用 \0027\xE2\x80\x99符号 → 文本绘制为“0027”或“xE2x80x99”

  • 我的代码
    generateVideo() 函数
    void generateVideo(char sourceVideoPath[], char text[], int destinationFileName) {
        char line[1000];
        sprintf(line, "ffmpeg -i %s -vf \"drawtext=fontfile=/path/to/font/:text='%s':fontcolor=white:fontsize=28:borderw=2.8:x=(w-text_w)/2:y=(h-text_h)/2\" -codec:a copy ../%d.mp4", sourceVideoPath, text, destinationFileName);
        system(line);
    }
    
    我不知道问题是来自 FFmpeg 还是 Shell,但我暂时无法用引号绘制文本是一种痛苦。
    提前谢谢你们!

    最佳答案

    从您的问题中很难说出问题所在,但我相信在文本中的引号之前编码\之类的修复应该可以解决它。如果您从在 shell 中工作的命令行开始,然后尝试从您的 C 代码中使用“system”发出该命令,则可能会更容易提供帮助。这是一个 main.c,它演示了我认为应该起作用的方法:

    #import <stdio.h>
    #import <stdlib.h>
    
    int main(int argc, char **argv) {
        char *sourceVideoPath = "video";
        char *text = "text \\'quoted\\' text";
        int destinationFileName = 10;
        char line[1000];
        sprintf(line, "echo ffmpeg -i %s -vf \"drawtext=fontfile=/path/to/font/:text='%s':fontcolor=white:fontsize=28:borderw=2.8:x=(w-text_w)/2:y=(h-text_h)/2\" -codec:a copy ../%d.mp4", sourceVideoPath, text, destinationFileName);
        system(line);
    }
    

    关于c - 如何在 Shell FFmpeg 中转义单引号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71347013/

    相关文章:

    linux - 从 bash 脚本初始化 Mininet 拓扑

    regex - 当名称不包含某些单词时删除文件

    python - 如何保持 float / double 算术确定性?

    java - JNA 通过引用在 C 结构中使用多个 void 指针

    shell - 避免 shell 脚本中的交互模式

    bash - 最快的 grep

    linux - 在 bash 中暂时禁用 'set-e'/'set -o errexit'?

    c - 在 OS X 上生成 MD5 密码

    c - 对于字符串,查找并替换

    linux - 使用需要输入的命令在脚本内部执行 sudo (bash)