简体   繁体   English

连接 mp4 视频后的 ffmpeg-python drawtext

[英]ffmpeg-python drawtext after concatenating mp4 videos

I realize a similar question has been asked here, so before pointing to that question and answer , please understand that my question is different.我意识到这里已经提出了一个类似的问题,所以在指出那个问题和答案之前,请理解我的问题是不同的。 I'm hoping that someone can point me in the right direction.我希望有人能指出我正确的方向。

In short, I have 5 video segments that I'm concatenating and reordering based on user input.简而言之,我有 5 个视频片段,根据用户输入将它们连接起来并重新排序。 Is there a way that I can drawtext dynamically over the input without having to process the video a second time?有没有一种方法可以在输入上动态绘制文本而无需第二次处理视频?

This is the code that I have which is working, but as you can see, I need to open the concatenated file and apply the text over that version.这是我正在运行的代码,但正如您所看到的,我需要打开连接的文件并将文本应用于该版本。 Then the file is saved as a duplicate.然后将文件另存为副本。

I'm looking for a more elegant way to accomplish this.我正在寻找一种更优雅的方式来实现这一点。 Any suggestions would be appreciated.任何建议,将不胜感激。

        video1 = ffmpeg.input('./assets/v_1.mp4')
        video2 = ffmpeg.input('./assets/v_2.mp4')
        video3 = ffmpeg.input('./assets/v_3.mp4')
        video4 = ffmpeg.input('./assets/v_4.mp4')
        video5 = ffmpeg.input('./assets/v_5.mp4')



        print(row)

        ## IF Row 1 and 2 have values they get all five.
        if row[1] == '1' and row[2] == '1':
            print("Matches here");
            outfile = row[0]+'.mp4'
            ##DO Stuff
            joined = ffmpeg.concat(video1.video,video1.audio,video2.video,video2.audio,video3.video,video3.audio,video4.video,video4.audio,video5.video,video5.audio, v=1,a=1,unsafe=1).node
            vj = joined[0]
            va = joined[1].filter('volume', 1)

            out = ffmpeg.output(vj,va, outfile)
            out.run()
            ## Once Concat Video is finished, then it draws text over the video. 
            input2 = ffmpeg.input(row[0]+'.mp4').drawtext(fontfile='/Users/jserra/Library/Fonts/Cocogoose-Condensed-Regular-trial.ttf',fontsize='60',timecode='00:00:00:00',r=60,text=row[0],fontcolor='black',escape_text=True)
            ffmpeg.output(input2,row[0]+'_1.mp4').run()

I've tried this and receive the following error:我试过这个并收到以下错误:

video1 = ffmpeg.input('./assets/StMarys_1.mp4').drawtext(fontfile='/Users/jserra/Library/Fonts/Cocogoose-Condensed-Regular-trial.ttf',fontsize='60',timecode='00:00:00:00',r=60,text=row[0],fontcolor='black',escape_text=True)

Error:错误:

    .virtualenvs/cvtesting/lib/python3.6/site-packages/ffmpeg/_run.py", line 93, in _allocate_filter_stream_names
    upstream_node, upstream_label
ValueError: Encountered drawtext(fontcolor='black', fontfile='/Users/jserra/Library/Fonts/Cocogoose-Condensed-Regular-trial.ttf', fontsize='60', r=60, text='jack', timecode='00:00:00:00') <1d2ff6bbf3f0> with multiple outgoing edges with same upstream label None; a `split` filter is probably required

I've also tried chaining it after the videos are concatenated with joined .我也试着链接它的视频与级联后joined I still receive errors.我仍然收到错误。

joined = ffmpeg.concat(video1.video,video1.audio,video2.video,video2.audio,video3.video,video3.audio,video4.video,video4.audio,video5.video,video5.audio, v=1,a=1,unsafe=1).drawtext(fontfile='/Users/jserra/Library/Fonts/Cocogoose-Condensed-Regular-trial.ttf',fontsize='60',timecode='00:00:00:00',r=60,text=row[0],fontcolor='black',escape_text=True).node

Will I need to process these videos twice?我需要处理这些视频两次吗? If there are any optimizations that I can make please let me know.如果我可以进行任何优化,请告诉我。 Also, if there are any pointers about displaying the drawn text for a certain period of time, the documentation seems kinda spotty as it relates to controlling the duration, I'm not sure what the values mean or how they impact each other.此外,如果有任何关于在一段时间内显示绘制文本的指示,文档似乎有点参差不齐,因为它与控制持续时间有关,我不确定这些值的含义或它们如何相互影响。

Thanks谢谢

Ok, so for those that experience issues following examples that aren't a 1:1 translation, this is what I've realized.好的,对于那些在不是 1:1 翻译的示例之后遇到问题的人,这就是我所意识到的。

If I apply the drawtext filter to the video that is returned in joined[0], I can add the text in at the right spot without encoding or processing the video twice.如果我将 drawtext 过滤器应用于在 join[0] 中返回的视频,我可以在正确的位置添加文本,而无需对视频进行两次编码或处理。

I'm assuming this has to do with the fact that drawtext can only be applied to videos and not audio (which makes sense).我假设这与 drawtext 只能应用于视频而不是音频(这是有道理的)的事实有关。

## IF Row 1 and 2 have values they get all five.
        if row[1] == '1' and row[2] == '1':
            print("Matches here");
            outfile = row[0]+'.mp4'

            ##DO Stuff
            joined = ffmpeg.concat(video1.video,video1.audio,video2.video,video2.audio,video3.video,video3.audio,video4.video,video4.audio,video5.video,video5.audio, v=1,a=1,unsafe=1).node

            print(type(joined))
            print(joined);
            vj = joined[0].drawtext(fontfile='/Users/js/Library/Fonts/Cocogoose-Condensed-Regular-trial.ttf',fontsize='600',x=100,y=10,text=row[0],fontcolor='white',escape_text=True,enable='between(t,1,2.5)')
            va = joined[1].filter('volume', 1)

            out = ffmpeg.output(vj,va, outfile)
            out.run()

I'm sure this isn't a thorough explaination, but it appears to be what makes sense in light of the test that I've run.我确信这不是一个彻底的解释,但根据我运行的测试,这似乎是有意义的。 In my first example outlining the problem, audio was removed from the video after it was processed the second time.在我概述问题的第一个示例中,音频在第二次处理后从视频中删除。 This is what gave me the idea to apply the drawtext filter to only the video returned in joined[0] since it appeared to be competing with the audio.这就是让我想到将 drawtext 过滤器仅应用于 join joined[0]返回的视频的原因,因为它似乎与音频竞争。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM