ffmpeg-android-java icon indicating copy to clipboard operation
ffmpeg-android-java copied to clipboard

Add audio to video together with image overlays

Open sasos90 opened this issue 8 years ago • 17 comments

I created small video editing tool with which you can put some overlay images to the video and show them at specific locations and durations (filter: overlay=enabled(from, to...), etc.). Now I also want to add some short audio sounds on the same way like pictures -> adding them to a specific time in the video. That means that overlays and audio sounds should be in one command if possible. I am using amix to merge all sound channels (video.mp4 and sound.mp3) but the thing is that they both start at the beginning. I tried with adelay=1500 but then I actually delay one channel that means I hear duplicate sounds with 1.5 sec delay. So I would like to delay only sound.mp3 input. Am I doing something wrong?

My try was that:

-i video.mp4 -i sound.mp3 -filter_complex amix -preset ultrafast -vcodec libx264 -r 24 -profile:v baseline -threads 14 video_output.mp4

Thank you very much!

sasos90 avatar May 17 '16 12:05 sasos90

Hey @sasos90 I'm also trying to overlay images to a video but the output video is never longer than one second. Ex.: input video is 5 seconds long but output has only 1 second. Have you faced any similar issue? The command I'm running is this: -y -i input.mp4 -i watermark.jpg -filter_complex overlay=main_w-overlay_w-10:main_h-overlay_h-10 -strict -2 -r 30 output.mp4

Any help is much appreciated!

mateusgrb avatar May 17 '16 18:05 mateusgrb

I think you need to add -vcodec copy before output to not re-encode the video stream. So in your case: -y -i input.mp4 -i watermark.jpg -filter_complex overlay=main_w-overlay_w-10:main_h-overlay_h-10 -strict -2 -r 30 -vcodec copy output.mp4

But I am not sure if it is -vcodec or something else. I will let you know at tomorrow morning because I need to check it.

sasos90 avatar May 17 '16 20:05 sasos90

thanks @sasos90 . I tried that but got the following error:

Streamcopy requested for output stream 0:0, which is fed from a complex filtergraph. Filtering and streamcopy cannot be used together.

Apparently I cannot use filter_complex and copy in the same command =( If you have any further guesses, please don't hesitate to share =)

mateusgrb avatar May 17 '16 21:05 mateusgrb

Oh that is true actually. Try this one: -vcodec libx264

sasos90 avatar May 18 '16 05:05 sasos90

No luck =( I'll create a new ticket for that, thanks for your help.

mateusgrb avatar May 18 '16 13:05 mateusgrb

Ok here is my working command for overlays: -i /storage/emulated/0/Download/test.mp4 -i /storage/emulated/0/Download/overlayone.png -i /storage/emulated/0/Download/overlaytwo.png -filter_complex overlay=52:275:enable='between(t,0.69,99.0)',overlay=510:71:enable='between(t,4.34,99.0)' -preset ultrafast -vcodec libx264 -r 24 -profile:v baseline -threads 14 -codec:a copy /storage/emulated/0/Download/test_edited.mp4

This one is working for me. Which error do you get?

sasos90 avatar May 18 '16 14:05 sasos90

@sasos90 Hey I am getting this error A/libc: Fatal signal 11 (SIGSEGV), code 1, fault addr 0x0 in tid 14661 (AsyncTask #4) by executing this command : ffmpeg -i video -loop 1 -t 2 -i image -filter_complex "[1:v][0:v][1:v]concat=n=3:v=1:a=0[v]" -map "[v]" out.mp4 Do you have any idea ? please help

nisd93 avatar Jul 29 '16 05:07 nisd93

Sure!

  1. First try to get rid of [...] brackets in your command. -> ffmpeg -i video -loop 1 -t 2 -i image -filter_complex concat=n=3:v=1:a=0 -map out.mp4
  2. Tell me what are you trying to do. Put the overlay on the video? Or what exactly?

sasos90 avatar Jul 29 '16 06:07 sasos90

@sasos90 Thanks for your reply and I am trying to overlay image to video for first 2 sec or 1 sec
I am using command like this someone from SO gave me this solution :

String[] complexCommand4={"ffmpeg", "-i", "/sdcard/videokit/in.mp4", "-loop", "1", "-t", "2", "-i", "/sdcard/videokit/water.jpg", "-filter_complex", "[1:v][0:v][1:v]concat=n=3:v=1:a=0[v]", "-map", "[v]", "/sdcard/videokit/out.mp4"};

Is it correct ?

Check this post I have described in my question

nisd93 avatar Jul 29 '16 06:07 nisd93

Ok first things first. For better readability of your command I would suggest you, that you write the command in one string, and split it by spaces when you provide it as an argument to the ffmpeg execution method.

You are using the concat filter which concat video files and not putting the overlay media to the video. There fore you need to use "overlay" filter which does that. So the command for you would be like this: "ffmpeg -i /sdcard/videokit/in.mp4 -i /sdcard/videokit/water.jpg -filter_complex overlay=100:150:enable='between(t,0.0,3.0)' -codec:a copy /sdcard/videokit/out.mp4"

In one of my previous posts, I provided an example of working command.

sasos90 avatar Jul 29 '16 09:07 sasos90

@sasos90 Sorry I have just misunderstood you by my sentence I shouldn't write overlay I want to add image to video when it starts and when it ends i want to add another image. Before I use command in one string but after seeing this I have changed in to peaces Btw Can you give me your email id !! if its possible for you so we can talk Thanks.

Edit : String[] command={"ffmpeg", "-i", "/sdcard/videokit/in.mp4","-i", "/sdcard/videokit/water.jpg", "-filter_complex", "overlay=100:150:enable='between(t,0.0,3.0)'", "-codec:a","copy", "/sdcard/videokit/out.mp4"}; From above command it adds image for 2 secs , Do you know how to add before and after video?

nisd93 avatar Jul 29 '16 10:07 nisd93

Hm. it would not really post my email here since it is not a private chat and unfortunately I can not find the private message feature on github :/

Ok the overlay is then for sure not the case. The concat SHOULD be ok I hope (at the moment I am not working on any android ffmpeg project so it is hard for me to test it). Just try your command without these [ bracket mappings ].

sasos90 avatar Jul 29 '16 10:07 sasos90

@sasos90 Ok no problem Thanks for your help. Last question after that command video quality has decreased, Do u have any idea ?

nisd93 avatar Jul 29 '16 11:07 nisd93

Try "-vcodec copy".

sasos90 avatar Jul 29 '16 11:07 sasos90

hello ,I try File file = new File(outputFile); if (file.exists()) { file.delete(); } String cmd = "-i " + totalFile; String input_file = ""; String adelay = ""; String s = ""; for (int i = 0; i < filePath.size(); i++) { input_file = input_file + " -i " + filePath.get(i); adelay = adelay + "[" + (i + 1) + ":a]adelay=" + fileName.get(i) + "[aud" + (i + 1) + "];"; s = s + "[aud" + (i + 1) + "]"; } cmd = cmd + input_file + " -preset superfast -async 1 -filter_complex " + adelay + "[0:a]" + s + "amix=inputs=" + (filePath.size() + 1) + ":duration=first:dropout_transition=0 -threads 0 " + outputFile; it is slow, Any help is much appreciated!

CodeK1988 avatar Mar 14 '17 08:03 CodeK1988

sasos90 Thank youuu soo much for your assistance.

NizNsm avatar Jul 12 '18 11:07 NizNsm

Ok here is my working command for overlays: -i /storage/emulated/0/Download/test.mp4 -i /storage/emulated/0/Download/overlayone.png -i /storage/emulated/0/Download/overlaytwo.png -filter_complex overlay=52:275:enable='between(t,0.69,99.0)',overlay=510:71:enable='between(t,4.34,99.0)' -preset ultrafast -vcodec libx264 -r 24 -profile:v baseline -threads 14 -codec:a copy /storage/emulated/0/Download/test_edited.mp4

This one is working for me. Which error do you get?

any idea how to add both image and text?

rocky8roy avatar Jul 21 '20 20:07 rocky8roy