When I execute ffmpeg command through java code then It is not executing properly
Example-:
我想通过 java 代码在视频中绘制文字
我的 Java 代码是
String cmd="ffmpeg -i 12213blob.mp4 -vf \"drawtext=fontfile=/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf:text='Text to write':fontsize=20:fontcolor=black:x=250:y=430\" output.mp4";
System.out.println(cmd);
Process p4 = Runtime.getRuntime().exec(cmd);
p4.waitFor();
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p4.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(p4.getErrorStream()));
// read the output from the command
System.out.println("Here is the standard output of the command:\n");
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
// read any errors from the attempted command
System.out.println("Here is the standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
}
catch (IOException e) {
System.out.println("exception happened - here's what I know: ");
e.printStackTrace();
}
}
当我执行此代码时,出现如下错误 这是命令的标准输出:
这是该命令的标准错误(如果有):
ffmpeg version 2.4.git Copyright (c) 2000-2014 the FFmpeg developers
built on Sep 16 2014 18:19:21 with gcc 4.8 (Ubuntu 4.8.2-19ubuntu1)
configuration: --prefix=/home/deepak/ffmpeg_build --extra-cflags=-I/home/deepak/ffmpeg_build/include --extra-ldflags=-L/home/deepak/ffmpeg_build/lib --bindir=/home/deepak/bin --enable-gpl --enable-libass --enable-libfdk-aac --enable-libfreetype --enable-libmp3lame --enable-libopus --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-nonfree --enable-x11grab
libavutil 54. 7.100 / 54. 7.100
libavcodec 56. 1.100 / 56. 1.100
libavformat 56. 4.101 / 56. 4.101
libavdevice 56. 0.100 / 56. 0.100
libavfilter 5. 1.100 / 5. 1.100
libswscale 3. 0.100 / 3. 0.100
libswresample 1. 1.100 / 1. 1.100
libpostproc 53. 0.100 / 53. 0.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '12213blob.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf55.48.100
Duration: 00:00:09.94, start: 0.046440, bitrate: 593 kb/s
Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 660x500 [SAR 1:1 DAR 33:25], 493 kb/s, 42.83 fps, 42.83 tbr, 16448 tbn, 85.67 tbc (default)
Metadata:
handler_name : VideoHandler
Stream #0:1(und): Audio: aac (mp4a / 0x6134706D), 44100 Hz, mono, fltp, 96 kb/s (default)
Metadata:
handler_name : SoundHandler
**[NULL @ 0x302bd40] Unable to find a suitable output format for 'to'
to: Invalid argument**
但是当我直接在终端上执行此命令时,它就能正确执行
答案1
String cmd="ffmpeg -i 12213blob.mp4 -vf drawtext=fontfile=/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf:text=Text-to-write:fontsize=20:fontcolor=black:x=250:y=430 output.mp4";
@user2511474:试试上面的命令,它会起作用。我最近遇到了类似的问题,通过删除“转义引号”和空格解决了它。