Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/main/scala/com/github/windymelt/zmm/Cli.scala
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class Cli(
scala.xml.XML.loadFile(filePath),
),
)
val (width, height) = (1080, 1920)

for {
_ <- logger.debug(s"generate($filePath, $outPathString)")
Expand Down Expand Up @@ -116,7 +117,7 @@ class Cli(
"Generating video and concatenated audio",
).use { _ =>
val paths = voices.map(_._1)
generateVideo(sayCtxPairs, paths) product ffmpeg
generateVideo(sayCtxPairs, paths, width, height) product ffmpeg
.concatenateWavFiles(paths.map(_.toString))
}
zippedVideo <- backgroundIndicator("Zipping silent video and audio").use {
Expand Down Expand Up @@ -152,6 +153,8 @@ class Cli(
ffmpeg.composeVideoWithDuration(
zippedVideo,
reductedVideoWithDuration,
width,
height,
)
}
}
Expand Down Expand Up @@ -394,6 +397,8 @@ class Cli(
private def generateVideo(
sayCtxPairs: Seq[(domain.model.Say, Context)],
paths: Seq[fs2.io.file.Path],
width: Int,
height: Int
): IO[os.Path] = {
import cats.syntax.parallel._

Expand Down Expand Up @@ -424,6 +429,8 @@ class Cli(
),
ss.takeScreenShot(
os.pwd / os.RelPath(htmlFile.toString),
windowWidth = 1080,
windowHeight = 1920,
),
)
} yield screenShotFile
Expand All @@ -437,6 +444,8 @@ class Cli(
}.parSequence
concatenatedImages <- ffmpeg.concatenateImagesWithDuration(
sceneImages.zip(sayCtxPairs.map(_._2.duration.get)),
width,
height,
)
} yield concatenatedImages

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ trait FFmpeg {
def getWavDuration(file: File): IO[FiniteDuration]
def concatenateImagesWithDuration(
imageDurationPair: Seq[(os.Path, FiniteDuration)],
width: Int,
height: Int,
): IO[os.Path]
def zipVideoWithAudioWithDuration(
videoPath: os.Path,
Expand All @@ -20,6 +22,8 @@ trait FFmpeg {
def composeVideoWithDuration(
baseVideoPath: os.Path,
overlayVideoDurationPair: Seq[(Option[os.Path], FiniteDuration)],
width: Int,
height: Int,
): IO[os.Path]
def zipVideoWithAudio(videoPath: os.Path, audioPath: os.Path): IO[os.Path]
def generateSilentWav(path: os.Path, length: FiniteDuration): IO[os.Path]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ class ConcreteFFmpeg(

def concatenateImagesWithDuration(
imageDurationPair: Seq[(os.Path, FiniteDuration)],
width: Int = 1920,
height: Int = 1080,
): IO[os.Path] = {
val writeCutfile = {
val cutFileContent = imageDurationPair map { case (p, dur) =>
Expand Down Expand Up @@ -124,11 +126,11 @@ class ConcreteFFmpeg(
"artifacts/cutFile.txt",
"-filter_complex",
// FIXME: 現在Chromiumのバグでサイズがおかしくなっているのでscaleしている
"split[img][img2];[img2]alphaextract,scale=1920:1080[alpha];[img]scale=1920:1080[scaledimg]",
s"split[img][img2];[img2]alphaextract,scale=${width}:${height}[alpha];[img]scale=${width}:${height}[scaledimg]",
"-pix_fmt",
"yuv420p",
"-c:v",
"libx264",
h264CodecName,
"-map",
"[scaledimg]",
"-map",
Expand All @@ -139,6 +141,23 @@ class ConcreteFFmpeg(
} yield os.pwd / os.RelPath("artifacts/scenes.mkv")
}

lazy val h264CodecName: String = {
val result = os.proc(ffmpegCommand, "-codecs").call(cwd = os.pwd)
// use "h264" or "libx264"
val h264Regex =
"""\s+DEV.LS\s+(\w+)\s+H.264\s+.+""".r.unanchored
val libx264Regex =
"""\s+DEV.LS\s+(\w+)\s+libx264\s+.+""".r.unanchored
result.out.text() match {
case h264Regex(codec) => codec
case libx264Regex(codec) => codec
case _ =>
throw new RuntimeException(
"H.264 codec not found in ffmpeg codecs list.",
)
}
}

def zipVideoWithAudioWithDuration(
videoPath: os.Path,
audioDurationPair: Seq[(Option[os.Path], FiniteDuration)],
Expand Down Expand Up @@ -210,6 +229,8 @@ class ConcreteFFmpeg(
def composeVideoWithDuration(
overlayVideoPath: os.Path,
baseVideoDurationPair: Seq[(Option[os.Path], FiniteDuration)],
width: Int = 1920,
height: Int = 1080,
): IO[os.Path] = {
import cats.implicits._

Expand All @@ -233,9 +254,18 @@ class ConcreteFFmpeg(
"-t",
paddingDur,
"-filter_complex",
s"smptehdbars=s=1920x1080:d=$paddingDur, fps=$FRAME_RATE_FPS[v];anullsrc=channel_layout=stereo:sample_rate=24000[o]",
s"smptehdbars=s=${width}x${height}:d=$paddingDur, fps=$FRAME_RATE_FPS[v];anullsrc=channel_layout=stereo:sample_rate=24000[o]",
"-safe",
"0",
"-i",
"artifacts/cutFile.txt",
"-filter_complex",
// FIXME: 現在Chromiumのバグでサイズがおかしくなっているのでscaleしている
s"split[img][img2];[img2]alphaextract,scale=${width}:${height}[alpha];[img]scale=${width}:${height}[scaledimg]",
"-pix_fmt",
"yuv420p",
"-c:v",
"libsvtav1",
"-map",
"[v]",
"-map",
Expand Down Expand Up @@ -317,7 +347,7 @@ class ConcreteFFmpeg(
"-filter_complex",
// TODO: overlayVideoPathのFPSが25になっているので30に持ち上げる
// 必ずbase videoはoverlay video以上の長さである必要があるので、何もない画面にbase videoをoverlayすることで長さを揃えてから再度overlayする
s"""nullsrc=s=1920x1080:r=$FRAME_RATE_FPS:d=$wholeDurationSec[nullsrc];
s"""nullsrc=s=${width}x${height}:r=$FRAME_RATE_FPS:d=$wholeDurationSec[nullsrc];
[0:a][1:a]amix=normalize=0[a];
[nullsrc][1:v]overlay=x=0:y=0[paddedbase];
[0:0][0:1]alphamerge[overlayv];
Expand Down Expand Up @@ -345,7 +375,7 @@ class ConcreteFFmpeg(
base,
"-filter_complex",
// 必ずbase videoはoverlay video以上の長さである必要があるので、何もない画面にbase videoをoverlayすることで長さを揃えてから再度overlayする
s"""nullsrc=s=1920x1080:r=$FRAME_RATE_FPS:d=$wholeDurationSec[nullsrc];
s"""nullsrc=s=${width}x${height}:r=$FRAME_RATE_FPS:d=$wholeDurationSec[nullsrc];
[0:a][1:a]amix=normalize=0[a];
[nullsrc][1:v]overlay=x=0:y=0[paddedbase];
[0:v]colorkey=0xFF00FF:0.1:0.5[overlayv];
Expand Down
Loading