【问题标题】:How to add text to a video with ffmpeg and python如何使用 ffmpeg 和 python 在视频中添加文本
【发布时间】:2012-01-01 05:38:25
【问题描述】:

我一直在尝试使用 ffmpeg 将文本添加到 avi 中,但我似乎无法做到正确。

请帮忙:

import subprocess

ffmpeg = "C:\\ffmpeg_10_6_11.exe"
inVid = "C:\\test_in.avi"
outVid = "C:\\test_out.avi"

proc = subprocess.Popen(ffmpeg + " -i " + inVid + " -vf drawtext=fontfile='arial.ttf'|text='test' -y " + outVid , shell=True, stderr=subprocess.PIPE)
proc.wait()
print proc.stderr.read()

【问题讨论】:

  • 那是错误信息?
  • 我最接近工作的是它给出了一条错误消息,说它无法打开字体文件。

标签: python video text ffmpeg drawtext


【解决方案1】:

在为drawtext 指定参数时,冒号“:”和反斜杠“\”具有特殊含义。所以你可以做的是通过将“:”转换为“\:”和“\”转换为“\\”来逃避它们。 您也可以将字体文件的路径用单引号括起来,以防路径包含空格。

这样你就有了

ffmpeg -i C:\Test\rec\vid_1321909320.avi -vf drawtext=fontfile='C\:\\Windows\\Fonts\\arial.ttf':text=test vid_1321909320.flv

【讨论】:

    【解决方案2】:

    哈哈

    原来 C:\Windows\Fonts 等中的双冒号“:”充当了拆分,所以当我输入字体的完整路径时,ffmpeg 正在读取我的命令,如下所示

    原始命令

    " -vf drawtext=fontfile='C:\\Windows\\fonts\\arial.ttf'|text='test' "
    

    ffmpeg的解读

    -vf drawtext=  # command
    
    fontfile='C    # C is the font file because the : comes after it signalling the next key
    
    arial.ttf'     # is the next key after fontfile = C (because the C is followed by a : signalling the next key)
    
    :text          # is the value the key "arial.tff" is pointing to
    
    ='test'        # is some arb piece of information put in by that silly user
    

    因此,要修复它,您需要删除字体文件路径中的 :。

    我的最终工作代码:

    import subprocess
    
    ffmpeg = "C:\\ffmpeg_10_6_11.exe"
    inVid = "C:\\test_in.avi"
    outVid = "C:\\test_out.avi"
    
    subprocess.Popen(ffmpeg + " -i " + inVid + ''' -vf drawtext=fontfile=/Windows/Fonts/arial.ttf:text=test ''' + outVid , shell=True)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-06-14
      • 2020-11-21
      • 1970-01-01
      • 2016-05-22
      • 2022-12-11
      • 2020-11-21
      • 2014-10-27
      • 2019-11-20
      相关资源
      最近更新 更多