The error you're encountering is due to the way the FFmpeg command is constructed in your code. The FFmpeg command you're generating is not specifying the correct filter when trying to burn the subtitles into the video. Specifically, the -vf (video filter) option is being passed only the subtitle file name without specifying the subtitles filter, which leads FFmpeg to interpret the subtitle file name as a filter name. This results in the error:
[AVFilterGraph @ ...] No such filter: 'videosubtitle...srt'
Error opening output files: Filter not found
To burn subtitles into a video using FFmpeg, you need to use the subtitles filter and specify the path to your subtitle file. The correct syntax for the FFmpeg command should include -vf "subtitles='path/to/subtitle.srt'".
Here's how you can fix your code:
Modify the Command Array to Include the Subtitles Filter
In your buildAndRunCommand function, you need to adjust the command array so that the subtitle file is passed correctly to the -vf option. Replace the following lines:
"-vf",
"${targetSubtitleFile.name}",
With:
"-vf",
"subtitles=${targetSubtitleFile.absolutePath}",
This change specifies that the subtitles filter should be used and provides the absolute path to the subtitle file.
Handle File Paths Correctly (Especially on Windows)
If you're running this code on Windows, you need to be careful with backslashes in file paths, which can be misinterpreted. You should replace backslashes with double backslashes or forward slashes to ensure that FFmpeg interprets the path correctly. Here's how you can adjust your code:
val subtitleFilePath = targetSubtitleFile.absolutePath.replace("\\", "\\\\")
Then update the command array:
"-vf",
"subtitles='${subtitleFilePath}'",
Alternatively, you can use forward slashes in the path:
val subtitleFilePath = targetSubtitleFile.absolutePath.replace("\\", "/")
Full Corrected Command Construction
Putting it all together, your command array should look like this:
val subtitleFilePath = targetSubtitleFile.absolutePath.replace("\\", "/")
val command = arrayOf(
"ffmpeg",
"-i",
videoFile.absolutePath,
"-vf",
"subtitles='${subtitleFilePath}'",
"-c:v",
selectedCodec,
"-b:v",
"$videoBitrate",
"-preset",
"slow",
"-c:a",
"copy",
outputFile.absolutePath
)
Example
Suppose your targetSubtitleFile.absolutePath is C:\Users\Irani\AppData\Local\Temp\videosubtitle123.srt. Replacing backslashes with forward slashes gives you C:/Users/Irani/AppData/Local/Temp/videosubtitle123.srt. Your FFmpeg command becomes:
ffmpeg -i "input_video.mp4" -vf "subtitles='C:/Users/Irani/AppData/Local/Temp/videosubtitle123.srt'" -c:v [selectedCodec] -b:v [videoBitrate] -preset slow -c:a copy "output_video_with_subs.mp4"
Additional Notes
-
Quoting Paths: It's important to wrap the subtitle file path in quotes if it contains spaces or special characters.
-
Handling Character Encodings: If your subtitles are not in UTF-8 encoding, you might need to specify the encoding in the subtitles filter, like so:
"-vf",
"subtitles='${subtitleFilePath}':charenc='WINDOWS-1252'",
Replace 'WINDOWS-1252' with the correct encoding as needed.
-
Escaping Characters: Make sure to properly escape any special characters in the file path.
Summary
The error occurs because FFmpeg expects a filter name after -vf, but it's receiving the subtitle file name instead. By specifying the subtitles filter and correctly formatting the file path, you instruct FFmpeg to apply the subtitles to the video.
Updated buildAndRunCommand Function
Here is the corrected function with all changes applied:
override fun buildAndRunCommand(videoFile: File, subtitleFile: File) {
val videoBitrate = streams.getVideoBitrate(videoFile)
val availableCodecs = codecs.getAvailableCodecs()
val outputFile = fileUtilities.generateUniqueFileName(
File(videoFile.parentFile, "${videoFile.nameWithoutExtension}_with_subs.mp4")
)
val targetSubtitleFile = File.createTempFile("videosubtitle${UUID.randomUUID()}", ".srt").also {
it.deleteOnExit()
}
subtitleFile.copyTo(targetSubtitleFile, true)
val charset = subtitleCodecs.charset(targetSubtitleFile)
logger.info(charset.toString())
val supportedSubtitleCodecs = subtitleCodecs.getSupportedSubtitleCodecs()
if (charset.toString() !in supportedSubtitleCodecs) {
subtitleCodecs.convertSubtitleIfNeeded(targetSubtitleFile, "srt", charset.toString())
}
val selectedCodec = userNotifier.codecSelection(availableCodecs)
logger.info(selectedCodec)
val subtitleFilePath = targetSubtitleFile.absolutePath.replace("\\", "/")
val command = arrayOf(
"ffmpeg",
"-i",
videoFile.absolutePath,
"-vf",
"subtitles='${subtitleFilePath}'",
"-c:v",
selectedCodec,
"-b:v",
"$videoBitrate",
"-preset",
"slow",
"-c:a",
"copy",
outputFile.absolutePath
)
commandRunner.runCommand(command, streams.getDuration(videoFile))
}
By making these changes, your FFmpeg command should execute correctly, and the subtitles will be burned into the video as intended.