Audio and video effects (fade-in, fade-out, volume changing?)

218 views
Skip to first unread message

Alexey Tsukanov

unread,
Aug 24, 2015, 7:42:12 PM8/24/15
to javacv
Hello everybody!

I need to concatenate a multiple video and merge it with audio. So, I did it, but another one problem is to set effect for video and audio.
Can anyone tell me, how to create fade-in and fade-out effects for video and audio?

The code below is how I'm releasing multiple concatenation and audio merging.
I'm hope very much for your help and would really appreciate for that!

public class MultipleVideoConcatenation {
Context context;
private String resultFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" +
DataManager.getInstance().getAuthorizationData().getUserId() + "/ResultVideoFile/ResultVideoFile.mp4";
private String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" +
DataManager.getInstance().getAuthorizationData().getUserId() + "/VideoPieces";
private String startBumper = Environment.getExternalStorageDirectory().getAbsolutePath() + "/start_bumper.mp4";
private String endBumper = Environment.getExternalStorageDirectory().getAbsolutePath() + "/end_bumper.mp4";
private String sourceAudio = Environment.getExternalStorageDirectory().getAbsolutePath() + "/sound1.mp3";
List<File> treeCollection;
long timeStamp = 0, tsStartBumper = 0, tsEndBumper = 0, tsPieces = 0, timeStampAudio = 0;

public MultipleVideoConcatenation(){}
public MultipleVideoConcatenation(Context context){
this.context = context;
}

public void concatenate(){
try{
File parentDirectory = new File(path);
treeCollection = getListFiles(parentDirectory);

List<FrameGrabber> frameGrabbers = new ArrayList<FrameGrabber>();
for (File ff : treeCollection) {
FrameGrabber grabber = new FFmpegFrameGrabber(ff);
grabber.setAudioChannels(2);
grabber.start();
frameGrabbers.add(grabber);
}
File startBumperFile = new File(startBumper);
File endBumperFile = new File(endBumper);
File audioFile = new File(sourceAudio);

FrameGrabber startBumperGrabber = new FFmpegFrameGrabber(startBumperFile);
startBumperGrabber.start();

FrameGrabber endBumperGrabber = new FFmpegFrameGrabber(endBumperFile);
endBumperGrabber.start();

FFmpegFrameGrabber audioGrabber = new FFmpegFrameGrabber(audioFile);
audioGrabber.setAudioChannels(2);
audioGrabber.start();

FrameRecorder recorder = new FFmpegFrameRecorder(resultFile, frameGrabbers.get(0).getImageWidth(), frameGrabbers.get(0).getImageHeight(), 2);
recorder.setVideoQuality(0);
recorder.setVideoCodec(13);
recorder.start();
Frame frame;
int k = 0;
while ((frame = startBumperGrabber.grabFrame()) != null) {
if(frame.image != null) {
recorder.record(frame);
}
tsStartBumper = startBumperGrabber.getTimestamp();
Log.i("LOG FOR", "START WRITING BUMPER");
}
timeStamp += tsStartBumper;
startBumperGrabber.stop();

for (FrameGrabber fg : frameGrabbers) {
while ((frame = fg.grabFrame()) != null) {
if(frame.image != null) {
recorder.record(frame);
}
tsPieces = fg.getTimestamp();
Log.i("LOG FOR", String.valueOf(k));
}
k++;
timeStamp += tsPieces;
if (k == frameGrabbers.size()) {
fg.stop();
Log.i("LOG FOR", "STOP");
}
}
while ((frame = endBumperGrabber.grabFrame()) != null) {
if(frame.image != null) {
recorder.record(frame);
}
tsEndBumper = endBumperGrabber.getTimestamp();
Log.i("LOG FOR", "START WRITING BUMPER");
}
endBumperGrabber.stop();
timeStamp += tsEndBumper - 2000000;
for(int j = 0; timeStampAudio <= timeStamp; j++){
frame = audioGrabber.grabFrame();
if(frame == null){
break;
}
frame.image = null;
timeStampAudio = audioGrabber.getTimestamp();
recorder.setTimestamp(timeStampAudio);
recorder.record(frame);
Log.i("LOG FOR", "Audio-Video merging22222222");
}
recorder.stop();
recorder.release();
audioGrabber.stop();

Log.i("LOG FOR", "STOP");
} catch (FrameGrabber.Exception | FrameRecorder.Exception e) {
e.printStackTrace();
}
}


public List<File> getListFiles(File parentDirectory){
ArrayList<File> inFiles = new ArrayList<File>();
File[] files = parentDirectory.listFiles();
for(File file : files){
if(!file.isDirectory()) {
inFiles.add(file);
}
else{
inFiles.addAll(getListFiles(file));
}
}
return inFiles;
}
}

Alexey Tsukanov

unread,
Aug 25, 2015, 1:57:41 AM8/25/15
to javacv
The problem with audio effects could be resolved by changing the volume of the audio at the beggining and at the end of it. I found this code from issue https://groups.google.com/forum/#!topic/javacv/qVVFUgHAm2Y 
float halfVolume = 0.1f;
Buffer[] sample = frame.samples;
if (sample != null) {
Buffer b = frame.samples[0];
for (int t = 0; t < b.capacity(); t++) { 
((FloatBuffer) b).put(t, ((FloatBuffer) b).get(t) * halfVolume); 
frame.samples = b;
rec.record(frame);
}

But it doesn't work. Is there any idea?


Samuel Audet

unread,
Aug 25, 2015, 10:46:36 AM8/25/15
to jav...@googlegroups.com
It should work. What happens?

As for video frames, try the new FFmpegFrameFilter I've just added here:
https://github.com/bytedeco/javacv/issues/164#ref-commit-05231a3

Samuel

Alexey Tsukanov

unread,
Sep 1, 2015, 5:01:35 AM9/1/15
to javacv
When I'm using the code with audio volume, I'm getting an error at this place:
frame.samples = b;
It says, that i need to use Buffer[] instead of Buffer

Samuel, as I understood, I need to download your project again?


вторник, 25 августа 2015 г., 20:46:36 UTC+6 пользователь Samuel Audet написал:

Samuel Audet

unread,
Sep 4, 2015, 8:21:34 AM9/4/15
to jav...@googlegroups.com
On 09/01/2015 06:01 PM, Alexey Tsukanov wrote:
> When I'm using the code with audio volume, I'm getting an error at this
> place:
> frame.samples = b;
> It says, that i need to use Buffer[] instead of Buffer

We don't need that line. Just remove it.

>
> Samuel, as I understood, I need to download your project again?

No, you could simply add FFmpegFrameFilter.java to your source files.

Samuel
Reply all
Reply to author
Forward
0 new messages