a-schild / jave2

The JAVE (Java Audio Video Encoder) library is Java wrapper on the ffmpeg project
GNU General Public License v3.0
1.24k stars 247 forks source link

Cannot add audio (.wav) to a video file (.mp4) #204

Open dr-slurp opened 2 years ago

dr-slurp commented 2 years ago

Hey! Overall I love jave, had tons of luck with splitting .mp4s. Now I need to add audio to my video files.

`attributes = new EncodingAttributes(); attributes.setOutputFormat("mp4"); attributes.setVideoAttributes(video); attributes.setAudioAttributes(audio);

    MultimediaObject originalVideo = new MultimediaObject(new File(pathToVideo));
    MultimediaObject originalAudio = new MultimediaObject(new File(pathToAudio));
    ArrayList<MultimediaObject> sources = new ArrayList<MultimediaObject>();
    sources.add(originalVideo);
    sources.add(originalAudio);

    File target = new File(pathToCombined);

    encoder.encode(sources, target, attributes);`

and I'm getting a
Exception in thread "main" ws.schild.jave.EncoderException: Exit code of ffmpeg encoding run is 1 at ws.schild.jave.Encoder.encode(Encoder.java:638) at ws.schild.jave.Encoder.encode(Encoder.java:481) at ws.schild.jave.Encoder.encode(Encoder.java:324) at dr_slurp.MyceliumFidelity.Video.VideoSplitter.addAudioToVideo(VideoSplitter.java:92) at dr_slurp.MyceliumFidelity.MyFiTester.audioAdder(MyFiTester.java:34) at dr_slurp.MyceliumFidelity.MyFiTester.main(MyFiTester.java:26)

Is there a way to put audio on a video file that has no audio already?

Thanks,

dr-slurp commented 2 years ago

FYI, I am able to add audio to the video if I use this command in the CLI: ffmpeg -i v.mp4 -i a.wav -c:v copy -map 0:v:0 -map 1:a:0 new.mp4 Is there a way to run arbitrary ffmpeg commands from jave2, or a way to run a similar command programatically?

THANKS!

DoreamonWdy commented 2 years ago

I also wanna to implement it, but i don't find any method. so sad..

dr-slurp commented 2 years ago

@wxyleo This is how i'm doing it without jave2, good luck

`

    Runtime rt = Runtime.getRuntime();
    Process pr = rt.exec("/opt/homebrew/bin/ffmpeg -i "+pathToVideo+" -i "+pathToAudio+" -c:v copy -map 0:v:0 -map 1:a:0 "+pathToCombined);

    BufferedReader stdInput = new BufferedReader(new InputStreamReader(pr.getInputStream()));
    BufferedReader stdError = new BufferedReader(new InputStreamReader(pr.getErrorStream()));

    String s = null;
    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);
    }

// Process pr = rt.exec("/opt/homebrew/bin/ffmpeg"); System.out.println(pr.getOutputStream().toString()); int result = pr.waitFor(); if(result == 0) { System.out.println("success"); }`

DoreamonWdy commented 2 years ago

tks