How to add music in video using ffempeg

Posted By : Abhishek Sharma | 24-Dec-2018

1) Intraduction:-

FFmpeg is the multimedia framework which is able to decode or can encode or transcode, mux, demux, stream, filter and we can play good quality media which have been had created. ffempeg supports the most used formats up to the cutting edge. it does not matter whether they were designed by some standards committee, the community or a corporation. ffempeg is highly portable: FFmpeg compiles, runs, and passes our testing across Linux, Mac OS X, Microsoft Windows, the BSDs, Solaris, etc.FFmpeg is also used for adding music to the videos or we can use to change the format of the media. 

FFmpeg may be a free software package project consisting of an enormous software package suite of libraries and programs for handling video, audio, and alternative transmission files and streams.
ffmpeg is additionally a command-line tool that's accustomed convert the formats of audio or video. It may also be accustomed capture and inscribe numerous hardware and software package sources like a TV capture card
.

 

2) Prerequisities:

1) Java

2) FFempeg

3) NetBeans

 

3) Command line command to add music in the video:-

After running this command you can check the output file where you can see the music with the video. but there is one consequence of using this if audio is of large duration then the video will be struck after its saturation point and audio will go on till last.

 

ffmpeg -i <video-path> -i <audio-path> -c:v copy -c:a aac -strict  experimental -map 0:v:0 -map 1:a:0  <output-path>

 

Here we have to add path like /home/video/video.mp4 for video and audio like /home/music/music.mp3 and output path like /home/output/output.mp4. So for removing this problem, we are using "-shortest" within the command. This will stop when the shortest duration one ends whether it is video or audio.

 

ffmpeg -i <video-path> -i <audio-path> -c:v copy -c:a aac -shortest -strict  experimental -map 0:v:0 -map 1:a:0  <output-path>

 

4) Java code for adding music:-

 

 

 

 

 

 
public boolean addMusic(String videoInput, String audioInput, String output) {

		String[] command = new String[] { "ffmpeg", "-i", audioInput, "-i", videoInput, "-acodec", "copy", "-shortest",
				"-vcodec", "copy", output };
		ProcessBuilder pb = new ProcessBuilder(command);
		boolean exeCmdStatus = executeCMD(pb);
		return exeCmdStatus;
	}

	private boolean executeCMD(ProcessBuilder pb) {
		pb.redirectErrorStream(true);
		Process process = null;
		try {
			process = pb.start();
		} catch (Exception ex) {
			ex.printStackTrace();
			System.out.println("oops");
			process.destroy();
			return false;
		}
		// wait until the process is done
		try {
			log.info(":: adding music ::");
			process.waitFor();
		} catch (InterruptedException e) {
			e.printStackTrace();
			System.out.println("woopsy");
			process.destroy();
			return false;
		}
		log.info(":: music added ::");
		return true;
	}

 

5) Conclusion       

After executing all the command as given above you will see the file in the output directory which will contain audio with the video up to the length of video as well as we can convert the format of the video also.

 

6) Reference:-

1) https://stackoverflow.com/questions/11779490/how-to-add-a-new-audio-not-mixing-into-a-video-using-ffmpeg

 

 

 

 

About Author

Author Image
Abhishek Sharma

Abhishek is a Java Developer and worked on servlet, JSP, collection, JDBC and now working on Spring boot. He has certification of core java from Aptech Pitampura.

Request for Proposal

Name is required

Comment is required

Sending message..