class FfmpegVideoSubtitle(
private val commandRunner: CommandRunner = CommandRunnerImp(),
private val streams: Streams = FfprobeStreams(),
private val fileUtilities: FileUtilities = FileUtilities(),
private val codecs: Codecs = FfmpegVideoCodecs(),
private val userNotifier: UserNotifier = JOptionPaneUserNotifier(),
private val subtitleCodecs: SubtitleCodecs = FfmpegSubtitleCodecs()
) : VideoLegend {
private val logger = Logger.getLogger(this.javaClass.name)
code
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 command = arrayOf(
"ffmpeg",
"-i",
videoFile.absolutePath,
"-vf",
"subtitles=${targetSubtitleFile.absolutePath}",
"-c:v",
selectedCodec,
"-b:v",
"$videoBitrate",
"-preset",
"slow",
"-c:a",
"copy",
outputFile.absolutePath
)
commandRunner.runCommand(command, streams.getDuration(videoFile))
}
}
como corrigir??
The error you're encountering is due to how ffmpeg is interpreting the file path to your subtitles file. In your code, the subtitles filter is not correctly handling Windows file paths that contain backslashes (\), colons (:), or spaces. As a result, ffmpeg is misinterpreting the path, leading to the error:
code
Unable to parse option value "UsersIraniAppDataLocalTempvideosubtitle1f345c57-bd90-431e-80d2-c29d4d81b44718170660379795976944.srt" as image size
Error applying option 'original_size' to filter 'subtitles': Invalid argument
To fix this issue, you need to properly format the subtitles file path when passing it to the subtitles filter in ffmpeg. Here's how you can do it:
Replace Backslashes with Forward Slashes: Windows paths use backslashes (\), but ffmpeg prefers forward slashes (/). Replacing backslashes with forward slashes makes the path more compatible with ffmpeg.
Escape Colons and Special Characters: Colons (:) in the path need to be escaped because ffmpeg uses colons to separate options in filters. Escaping colons and other special characters prevents ffmpeg from misinterpreting the path.
Enclose the Path in Single Quotes: Wrapping the path in single quotes ensures that any spaces or special characters in the path are treated as part of the filename.
Add :safe=1 to the Filter: The safe option in the subtitles filter allows ffmpeg to accept filenames containing unsafe characters. Setting safe=1 enables this feature.
Here's the modified code with these changes:
kotlin
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)
// Adjust the subtitles file path for ffmpeg
val subtitlePathForFilter = targetSubtitleFile.absolutePath
.replace("\\", "/") // Replace backslashes with forward slashes
.replace(":", "\\:") // Escape colons
.replace("'", "\\'") // Escape single quotes if any
val subtitlesFilterOption = "subtitles='${subtitlePathForFilter}':safe=1"
val videoFilePath = videoFile.absolutePath.replace("\\", "/")
val outputFilePath = outputFile.absolutePath.replace("\\", "/")
val command = arrayOf(
"ffmpeg",
"-i",
videoFilePath,
"-vf",
subtitlesFilterOption,
"-c:v",
selectedCodec,
"-b:v",
"$videoBitrate",
"-preset",
"slow",
"-c:a",
"copy",
outputFilePath
)
commandRunner.runCommand(command, streams.getDuration(videoFile))
}
Explanation:
Path Adjustments: We adjust the paths of the video file, subtitles file, and output file to use forward slashes. We also escape any colons and single quotes in the subtitles path.
kotlin
val subtitlePathForFilter = targetSubtitleFile.absolutePath
.replace("\\", "/")
.replace(":", "\\:")
.replace("'", "\\'")
Subtitles Filter Option: We construct the subtitles filter option by enclosing the subtitles path in single quotes and adding :safe=1:
kotlin
val subtitlesFilterOption = "subtitles='${subtitlePathForFilter}':safe=1"
Command Array: We ensure that the paths for the video and output files are correctly formatted:
kotlin
val videoFilePath = videoFile.absolutePath.replace("\\", "/")
val outputFilePath = outputFile.absolutePath.replace("\\", "/")
Running the Command: We pass the adjusted paths to the ffmpeg command:
kotlin
val command = arrayOf(
"ffmpeg",
"-i",
videoFilePath,
"-vf",
subtitlesFilterOption,
// ... rest of the command
)
Note: By adjusting the paths and properly formatting the subtitles filter option, ffmpeg should now correctly locate and apply the subtitles file without encountering parsing errors.
Additional Tips:
Testing the Command: It's a good idea to print out the final command or execute it directly in the command line to ensure it works as expected.
Avoiding Spaces in Paths: If possible, avoid having spaces in file and directory names, as they can cause issues with command-line tools if not properly handled.
Error Handling: Consider adding more robust error handling to catch and log any exceptions that occur during the execution of the command.
Result:
With these changes, your application should successfully add subtitles to your video without encountering the "Unable to parse option value as image size" error.