Python 从歌词中创建图像序列以在ffmpeg中使用

Python 从歌词中创建图像序列以在ffmpeg中使用,python,ffmpeg,Python,Ffmpeg,我正在尝试用python制作一个MP3+歌词->MP4程序 我有这样一个歌词文件: [00:00.60]Revelation, chapter 4 [00:02.34]After these things I looked, [00:04.10]and behold a door was opened in heaven, [00:06.41]and the first voice which I heard, as it were, [00:08.78]of a trumpet spea

我正在尝试用python制作一个MP3+歌词->MP4程序

我有这样一个歌词文件:

[00:00.60]Revelation, chapter 4
[00:02.34]After these things I looked, 
[00:04.10]and behold a door was opened in heaven, 
[00:06.41]and the first voice which I heard, as it were, 
[00:08.78]of a trumpet speaking with me, said: 
[00:11.09]Come up hither, 
[00:12.16]and I will shew thee the things which must be done hereafter.
[00:15.78]And immediately I was in the spirit: 
[00:18.03]and behold there was a throne set in heaven, 
[00:20.72]and upon the throne one sitting.
[00:22.85]And he that sat, 
[00:23.91]was to the sight like the jasper and the sardine stone; 
[00:26.97]and there was a rainbow round about the throne, 
[00:29.16]in sight like unto an emerald.
[00:31.35]And round about the throne were four and twenty seats; 
[00:34.85]and upon the seats, four and twenty ancients sitting, 
[00:38.03]clothed in white garments, and on their heads were crowns of gold.
[00:41.97]And from the throne proceeded lightnings, and voices, and thunders; 
[00:46.03]and there were seven lamps burning before the throne, 
[00:48.60]which are the seven spirits of God. 
[00:51.23]And in the sight of the throne was, as it were, 
[00:53.79]a sea of glass like to crystal; 
[00:56.16]and in the midst of the throne, and round about the throne, 
[00:59.29]were four living creatures, full of eyes before and behind.
[01:03.79]And the first living creature was like a lion: 
from_second = counter/2
我试图从歌词中创建一系列图像,用于ffmpeg

os.system(ffmpeg_path + " -r 2 -i " + images_path + "image%1d.png -i " + audio_file + " -vcodec mpeg4 -y " + video_name)
我试着找出每行要制作的图像数量。我试着从当前行中减去下一行的秒数。它可以工作,但会产生非常不一致的结果

import os
import datetime
import time
import math
from PIL import Image, ImageDraw


ffmpeg_path = os.getcwd() + "\\ffmpeg\\bin\\ffmpeg.exe"
images_path = os.getcwd() + "\\test_output\\"
audio_file = os.getcwd() + "\\audio.mp3"
lyric_file = os.getcwd() + "\\lyric.lrc"

video_name = "movie.mp4"


def save():

    lyric_to_images()
    os.system(ffmpeg_path + " -r 2 -i " + images_path + "image%1d.png -i " + audio_file + " -vcodec mpeg4 -y " + video_name)


def lyric_to_images():

    file  = open(lyric_file, "r")

    data = file.readlines()

    startOfLyric = True
    lstTimestamp = []

    images_to_make = 0
    from_second = 0.0
    to_second = 0.0

    for line in data:
        vTime = line[1:9] # 00:00.60

        temp = vTime.split(':')

        minute = float(temp[0])
        #a = float(temp[1].split('.'))
        #second = float((minute * 60) + int(a[0]))
        second = (minute * 60) + float(temp[1])

        lstTimestamp.append(second)

    counter = 1

    for i, second in enumerate(lstTimestamp):

        if startOfLyric is True:
            startOfLyric = False
            #first line is always 3 seconds (images to make = 3x2)
            for x in range(1, 7):
                writeImage(data[i][10:], 'image' + str(counter))
                counter += 1
        else:
            from_second = lstTimestamp[i-1]
            to_second = second

            difference = to_second - from_second
            images_to_make = int(difference * 2)

            for x in range(1, int(images_to_make+1)):
                writeImage(data[i-1][10:], 'image'+str(counter))
                counter += 1

    file.close()

def writeImage(v_text, filename):

    img = Image.new('RGB', (480, 320), color = (73, 109, 137))

    d = ImageDraw.Draw(img)
    d.text((10,10), v_text, fill=(255,255,0))

    img.save(os.getcwd() + "\\test_output\\" + filename + ".png")


save()
有没有高效准确的方法来计算每行需要创建多少图像


注意:无论我创建多少个图像,都必须乘以2,因为我使用的是FFmpeg(2fps)的
-r2

好代码。最小的变化和最好的改进是根据文件中的当前时间位置从_秒开始计算,如下所示:

[00:00.60]Revelation, chapter 4
[00:02.34]After these things I looked, 
[00:04.10]and behold a door was opened in heaven, 
[00:06.41]and the first voice which I heard, as it were, 
[00:08.78]of a trumpet speaking with me, said: 
[00:11.09]Come up hither, 
[00:12.16]and I will shew thee the things which must be done hereafter.
[00:15.78]And immediately I was in the spirit: 
[00:18.03]and behold there was a throne set in heaven, 
[00:20.72]and upon the throne one sitting.
[00:22.85]And he that sat, 
[00:23.91]was to the sight like the jasper and the sardine stone; 
[00:26.97]and there was a rainbow round about the throne, 
[00:29.16]in sight like unto an emerald.
[00:31.35]And round about the throne were four and twenty seats; 
[00:34.85]and upon the seats, four and twenty ancients sitting, 
[00:38.03]clothed in white garments, and on their heads were crowns of gold.
[00:41.97]And from the throne proceeded lightnings, and voices, and thunders; 
[00:46.03]and there were seven lamps burning before the throne, 
[00:48.60]which are the seven spirits of God. 
[00:51.23]And in the sight of the throne was, as it were, 
[00:53.79]a sea of glass like to crystal; 
[00:56.16]and in the midst of the throne, and round about the throne, 
[00:59.29]were four living creatures, full of eyes before and behind.
[01:03.79]And the first living creature was like a lion: 
from_second = counter/2
在过滤器中使用字幕。这将比事先制作图像和尝试计时更容易、更有效。您还可以控制字体、大小、颜色、样式、位置等。例如,使用过滤器作为背景:

SRT 这是一种支持基本样式的简单格式

1
00:00:00,600 --> 00:00:02,340
Revelation, chapter 4

2
00:00:02,340 --> 00:00:04,100
<b>After</b> these <u>things</u> I <font color="green">looked</font>,

3
00:00:04,100 --> 00:00:06,410
and behold a door was opened in heaven,
这个示例只显示了格式结构:我没有添加任何样式。如果你想尝试这种格式,可以用它来创建ASS字幕
ffmpeg
可以转换字幕格式

force_样式
选项 字幕过滤器中的
force_style
选项可以扩展简化SRT格式的格式化可能性。它使用ASS格式选项,如
Fontsize
Fontname
outlinecolor
,等等。查看上面ASS示例中的
格式
行以获取选项列表

subtitles=lyrics.srt:force_style='Fontname=DejaVu Serif,PrimaryColour=&HCCFF0000'

为什么不用字幕而不用图片呢?您可以制作一个简单的SRT字幕文件,然后使用过滤器渲染它。@LordNeckbeard,这是一个很好的观点。我曾经想过使用SRT,但我需要能够控制字体名称、大小、颜色等。我不确定是否有可能实现这样的功能。我会调查的!