INDExOS / media-for-mobile

Media for Mobile
Other
456 stars 177 forks source link

Ask for help. How can I use one MediaComposer to cut three videos ,then add different effects like rotation effect for each one and join them? #32

Open jinCN opened 8 years ago

jinCN commented 8 years ago

I can do that by using tmp files. But how can I do it using one MediaComposer?

marlon-b commented 8 years ago

Yes, you can. For example you can take 3 videos with different camera rotation and join them with applying RotateEffect with different rotation angles

jinCN commented 8 years ago

@marlon-b Thank you. But how can I? For example, I can handle one source file using this:

        mediaComposer.addSourceFile(mediaUri1);
        mediaComposer.setTargetFile(dstMediaPath);

        configureVideoEncoder(mediaComposer, 480, 480);
        configureAudioEncoder(mediaComposer);

        IVideoEffect effect = new RotateEffect(video_rotation, factory.getEglUtil());
        mediaComposer.addVideoEffect(effect);
        MediaFile mediaFile = mediaComposer.getSourceFiles().get(0);
        mediaFile.addSegment(new Pair<Long, Long>(segmentFrom1, segmentTo1));

Then if I add

 mediaComposer.addSourceFile(mediaUri2);
 mediaComposer.addSourceFile(mediaUri3);

The same RotateEffect will be applied to the second and the third video. That's not what I want.

marlon-b commented 8 years ago

You have to create 3 RotateEffect objects with different angles. Set segment to every of them. Also add 3 source files with start, end positions if you want to cut. Please take care about start, end positions for effect and files segments.

marlon-b commented 8 years ago

I do not have a simple example by hand, but you can take a look at this as a reference:

int index = 0;

for (MediaFileParameters mediaFileParameters : listMediaFileParameters) {
    String fileName = "";
    fileName = mediaFileParameters.getFullPath();

    if (mediaFileParametersTarget.getEffect() > 0 || mediaFileParameters.getRotation() > 0) {
        IVideoEffect effect = null;
        switch (mediaFileParametersTarget.getEffect()){
            case 1:
                effect = new SepiaEffect(mediaFileParameters.getRotation()
                                        , androidMediaObjectFactory.getEglUtil());
                break;
            case 2:
                effect = new GrayscaleEffect(mediaFileParameters.getRotation()
                                            , androidMediaObjectFactory.getEglUtil());
                break;
            case 3:
                effect = new InverseEffect(mediaFileParameters.getRotation()
                                            , androidMediaObjectFactory.getEglUtil());
                break;
            case 0:
                if (mediaFileParameters.getRotation() > 0) {
                    effect = new RotateEffect(mediaFileParameters.getRotation()
                                             , androidMediaObjectFactory.getEglUtil());
                }
                break;
        }

        FileSegment fileSegment = new FileSegment((long) listPositionsInTarget.get(index).getStart() * 1000, (long) listPositionsInTarget.get(index).getEnd() * 1000);
        effect.setSegment(fileSegment);
        mediaComposer.addVideoEffect(effect);
    }

    try {
        mediaComposer.addSourceFile(fileName);
    }  catch (Exception e) {
        Toast.makeText(activity, e.getMessage(), Toast.LENGTH_LONG).show();
    } finally {
        MediaFile mediaFile = mediaComposer.getSourceFiles().get(mediaComposer.getSourceFiles().size() - 1);
        mediaFile.addSegment(new Pair<Long, Long>((long)mediaFileParameters.getStartPosition() * 1000, (long)mediaFileParameters.getEndPosition() * 1000));
        index++;
    }
}
jinCN commented 8 years ago

I believe this will help, but I still don't understand what the FileSegment is. If I have three 10s video, all cut to be from 5s to 6s. Then what the three FileSegments should be?

jinCN commented 8 years ago

@marlon-b Can you take a look at my problem?

marlon-b commented 8 years ago

please try to set 3 segments: 0, 1000

and for

mediaFile.addSegment

set 5000, 6000

jinCN commented 8 years ago

Then I got only the first rotated.

marlon-b commented 8 years ago

please show your code

jinCN commented 8 years ago

Are we too noisy here? The problem is almost solved. Maybe we can private chat. My code is like this:

        mediaComposer.setTargetFile(dstMediaPath);

        configureVideoEncoder(mediaComposer, 480, 480);
        configureAudioEncoder(mediaComposer);
long t=segmentTo-segmentFrom;
        IVideoEffect effect = new RotateEffect(90, factory.getEglUtil());
        effect.setSegment(new FileSegment(0, t));
        mediaComposer.addVideoEffect(effect);
        mediaComposer.addSourceFile(mediaUri1);

        MediaFile mediaFile = mediaComposer.getSourceFiles().get(0);
        mediaFile.addSegment(new Pair<Long, Long>(segmentFrom, segmentTo));

        effect = new RotateEffect(90, factory.getEglUtil());
        effect.setSegment(new FileSegment(0, t));
        mediaComposer.addVideoEffect(effect);
        mediaComposer.addSourceFile(mediaUri1);
        mediaFile = mediaComposer.getSourceFiles().get(1);
        mediaFile.addSegment(new Pair<Long, Long>(segmentFrom, segmentTo));

        effect = new RotateEffect(90, factory.getEglUtil());
        effect.setSegment(new FileSegment(0, t));
        mediaComposer.addVideoEffect(effect);
        mediaComposer.addSourceFile(mediaUri1);
        mediaFile = mediaComposer.getSourceFiles().get(2);
        mediaFile.addSegment(new Pair<Long, Long>(segmentFrom, segmentTo));
marlon-b commented 8 years ago

can you try to have three different effect objects, effect 1, effect 2 and effect 3?

if this does not help set 2000, 3000 to second segment and 3000, 4000 to the third

jinCN commented 8 years ago

I try it. That makes no difference

jinCN commented 8 years ago

@marlon-b And I found another problem related to this one. #38

polaris0227 commented 8 years ago

Hi, @mysyljr, @marlon-b. These days I'm making app to record and join videos, but I've got some problem. After transcode or join, audio of video is changed, speed seems like slower than original audio. Hope you to provide advice if possible. Regards.

jinCN commented 8 years ago

@polaris0227 This may be the problem relevant to the codec type, which requires specific information of your video file and the codec in use.

polaris0227 commented 8 years ago

@mysyljr , 感谢您的回复。 你启动过Github示例软件吗? 这一问题也出现在示例软件。 Thank you so much for your advice. But I'm not sure how to resolve this problem. Did you test sample app in Github? This problem also happens in sample. If I transcode mp4 file that was also recorded using this app, audio is changed. Hope to provide detailed advice. Regards.

On Sun, Jun 26, 2016 at 6:16 PM, 梁俊仁 notifications@github.com wrote:

@polaris0227 https://github.com/polaris0227 This may be the problem relevant to the codec type, which requires specific information of your video file and the codec in use.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/INDExOS/media-for-mobile/issues/32#issuecomment-228606176, or mute the thread https://github.com/notifications/unsubscribe/ANbfpItnKi9ooWjkdc8a3oLb5PmkmFxoks5qPpfNgaJpZM4IfBhO .

jinCN commented 8 years ago

@polaris0227 这我倒是没怎么注意,但我目前不太相信任何mp4文件都能出这个问题,我会回去试试的。

polaris0227 commented 8 years ago

谢谢。我在期待您的回答,试试以后请告诉我结果。

On Mon, Jun 27, 2016 at 5:42 AM, 梁俊仁 notifications@github.com wrote:

@polaris0227 https://github.com/polaris0227 这我倒是没怎么注意,但我目前不太相信任何mp4文件都能出这个问题,我会回去试试的。

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/INDExOS/media-for-mobile/issues/32#issuecomment-228643009, or mute the thread https://github.com/notifications/unsubscribe/ANbfpB_7CoB-d1xN1nefsQBP2I8z4Gakks5qPziOgaJpZM4IfBhO .

jinCN commented 8 years ago

我试了试,没有发现这个问题

polaris0227 commented 8 years ago

能送给我apk文件吗?让我试试。

On Tue, Jun 28, 2016 at 6:59 PM, 梁俊仁 notifications@github.com wrote:

我试了试,没有发现这个问题

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/INDExOS/media-for-mobile/issues/32#issuecomment-229095524, or mute the thread https://github.com/notifications/unsubscribe/ANbfpME9qmzzxH5bwspkE_GuBEAwgghcks5qQUTYgaJpZM4IfBhO .

polaris0227 commented 8 years ago

我的在结束录像的时候,常常出个错误。是不是您的也那样?

On Tue, Jun 28, 2016 at 8:51 PM, Polaris My mypolaris0227@gmail.com wrote:

能送给我apk文件吗?让我试试。

On Tue, Jun 28, 2016 at 6:59 PM, 梁俊仁 notifications@github.com wrote:

我试了试,没有发现这个问题

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/INDExOS/media-for-mobile/issues/32#issuecomment-229095524, or mute the thread https://github.com/notifications/unsubscribe/ANbfpME9qmzzxH5bwspkE_GuBEAwgghcks5qQUTYgaJpZM4IfBhO .

jinCN commented 8 years ago

我用的就是里面的sample,没有发现问题,你用的是android6吗? 录像功能我目前还没有用过

polaris0227 commented 8 years ago

我用的也是里面的sample。 你能不能送给我你的源码和apk文件?

On Wed, Jun 29, 2016 at 5:35 AM, 梁俊仁 notifications@github.com wrote:

我用的就是里面的sample,没有发现问题,你用的是android6吗? 录像功能我目前还没有用过

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/INDExOS/media-for-mobile/issues/32#issuecomment-229240888, or mute the thread https://github.com/notifications/unsubscribe/ANbfpOGCGhbmKaFE1HcVqoLib3jm7-hKks5qQdoIgaJpZM4IfBhO .