2

I'm processing a load of videos to concat together using Java on a Linux server.

First, I mix in an audio track with this process, depending on if the video already has audio or not.

ffmpeg -y -i video.mp4 -i  audio.mp3
 -filter_complex "[0:a]aresample=async=1,volume=1.0[a0]; 
 [1:a]aresample=async=1,volume=1.0[a1]; 
[a0][a1]amix=inputs=2[a] -map 0:v -map "[a]" -c:v copy
-c:a aac -strict -2 -shortest output.mp4

Or if there is no existing audio

ffmpeg -y -i video.mp4 -i  audio.mp3 -af apad 
-map 0:v -map 1:a -c:v copy -shortest output.mp4

With this code below in the Java application

        if(getVideoAttribute(video, "a:0", "duration") != null)
        pb = new ProcessBuilder(
                "ffmpeg", "-y", "-i", video.getAbsolutePath(),
                "-i", audio.getAbsolutePath(),
                "-filter_complex", "[0:a]aresample=async=1,volume=" + videoVolume + "[a0]; [1:a]aresample=async=1,volume=" + audioVolume + "[a1]; [a0][a1]amix=inputs=2[a]", "-map", "0:v", "-map", "[a]", "-c:v", "copy", "-c:a", "aac", "-strict", "-2", "-shortest",
                outFile.getAbsolutePath()
        );
    else
        pb = new ProcessBuilder(
                "ffmpeg", "-y", "-i", video.getAbsolutePath(),
                "-i", audio.getAbsolutePath(),
                "-af", "apad", "-map", "0:v", "-map", "1:a", "-c:v", "copy", "-shortest",
                outFile.getAbsolutePath()
        );

I then add all of the video files into a textfile, and concat that text file using

 ffmpeg -f concat -i list.txt -safe 0
            -c:v copy -c:a aac -strict experimental output.mp4

Using this Java below

    ProcessBuilder pb = new ProcessBuilder(
            "ffmpeg", "-f",
            "concat", "-i", Constants.TEMP_DIR_PATH + listFile.getName(),
            "-safe", "0",
            "-c:v","copy","-c:a","aac","-strict","experimental",
            Constants.TEMP_DIR_PATH + out.getName()
    );

In the final output, the audio is slightly out of sync, i.e. audio from one video runs over into the next video etc.

I checked the audio/video durations for each video and they are indeed slightly different (video duration is the first number, audio track second)

6.480000
6.528000
0e9e449b-f53e-473b-a91a-72e38ecfc789_3840.mp4
12.760000
12.736000
06bd1e70-b9aa-4c26-86d2-98fc421f6729_3840.mp4
17.520000
17.493000
f2dc7fc0-d4ac-4a82-88cb-777a16a1f818_3840.mp4
7.880000
7.850000
03681d53-4532-48e0-b3e4-cc1e7170ae65_3840.mp4
6.625000
6.592000
219d174d-9df6-41ec-b95e-2ec4137e07a4_3840.mp4
14.541992
14.485000

Which I assume is the cause of the problem.

Can anyone help me either change the way I mix them so that the video and audio is always the same length, or show me a way to use concat so it adds silence at the end of the audio so they are the same length?

3
  • Please don't show the Java stuff, but the actual ffmpeg command lines being run as well as the command line output from the terminal.
    – slhck
    Commented Aug 4, 2017 at 10:52
  • OK have extracted the FFMPEG commands
    – beek
    Commented Aug 4, 2017 at 22:22
  • … and the command line output from the terminal.
    – slhck
    Commented Aug 5, 2017 at 10:10

0

You must log in to answer this question.

Browse other questions tagged .