V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
qwertty01
V2EX  ›  问与答

我服了,使用 Java 和 Python 运行 ffmpeg 命令, Java 运行了大半天不出结果, Python 秒出

  •  1
     
  •   qwertty01 · 2020-10-18 11:27:49 +08:00 · 1958 次点击
    这是一个创建于 1279 天前的主题,其中的信息可能已经有所发展或是发生改变。

    大佬们,能不能说一下为什么这样啊 java

    public class TestApp {
        public static void main(String[] args) throws IOException, InterruptedException {
            ProcessBuilder processBuilder = new ProcessBuilder("D:\\ffmpeg\\bin\\ffmpeg.exe", "-i", "D:\\test\\test.mp4",
                    "-strict", "-2", "-vf", "crop=280:360:0:0", "D:\\test\\out.mp4"
            );
            Process process = processBuilder.start();
            process.waitFor();
        }
    }
    

    python

    os.system("D:\\ffmpeg\\bin\\ffmpeg.exe -i D:\\test\\test.mp4 -strict -2 -vf crop=280:360:0:0 D:\\test\\out.mp4")
    
    
    第 1 条附言  ·  2020-10-18 15:14:30 +08:00
    问题解决了, 设置 redirectErrorStream(true),然后再从 inputstream 不断读就行了

    ```
    package priv.sunmd.app

    import java.io.BufferedReader
    import java.io.IOException
    import java.io.InputStream
    import java.io.InputStreamReader

    object TestC {
    @Throws(IOException::class, InterruptedException::class)
    @JvmStatic
    fun main(args: Array<String>) {
    val processBuilder = ProcessBuilder(
    "D:\\ffmpeg\\bin\\ffmpeg.exe", "-i", "D:\\test\\test.mp4",
    "-strict", "-2", "-vf", "crop=280:360:0:0", "D:\\test\\out.mp4"
    )
    val process = processBuilder.redirectErrorStream(true).start()

    ReadStream(process.inputStream).run()
    }
    }

    internal class ReadStream(private val inputStream: InputStream) : Runnable {
    override fun run() {
    val bufferedReader = BufferedReader(
    InputStreamReader(inputStream)
    )
    try {
    var line: String?
    while (bufferedReader.readLine().also { line = it } != null) {
    println(line)
    }
    } catch (e: IOException) {
    e.printStackTrace()
    }
    }
    }
    ```
    xl224
        1
    xl224  
       2020-10-18 12:25:33 +08:00 via iPhone   ❤️ 1
    把 stdout 和 stderr 读出来,也可以设置转发 err 到 stdout 然后一起读取出来,不读取如果写满缓冲会挂起程序,ffmpeg 我记得会有大量输出信息
    paulluis2dev
        2
    paulluis2dev  
       2020-10-18 12:49:59 +08:00 via iPhone
    没用对,说明不了问题
    qwertty01
        3
    qwertty01  
    OP
       2020-10-18 13:26:08 +08:00
    @xl224 大佬这样写行吗?
    ```
    Process process = processBuilder.start();
    byte[] bytes = new byte[104];
    while (true) {
    if (process.getInputStream().read(bytes) != -1) {
    System.out.println(bytes.toString());
    } else {
    break;
    }
    }
    ```
    lxk11153
        4
    lxk11153  
       2020-10-18 13:27:24 +08:00
    -y (global)
    Overwrite output files without asking.
    chihiro2014
        5
    chihiro2014  
       2020-10-18 13:29:16 +08:00
    = =说起来,我服务端也是用 Java 调用 ffmpeg,完全没这种问题
    lxk11153
        6
    lxk11153  
       2020-10-18 13:40:29 +08:00
    -strict 拿来干嘛的?

    ----
    try(InputStream pis=process.getInputStream()){
    // org.apache.commons.io.IOUtils#toString(InputStream, Charset)
    }
    qwertty01
        7
    qwertty01  
    OP
       2020-10-18 16:59:32 +08:00
    @lxk11153 不知道,我直接在网上抄的命令 😂
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   952 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 22:27 · PVG 06:27 · LAX 15:27 · JFK 18:27
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.