Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No audio after Video Resample #1

Open
HardikSuba opened this issue Nov 18, 2015 · 15 comments
Open

No audio after Video Resample #1

HardikSuba opened this issue Nov 18, 2015 · 15 comments

Comments

@HardikSuba
Copy link

There is no audio in generated video file.
I have used this code,

VideoResampler resampler = new VideoResampler();
        resampler.addSamplerClip(new SamplerClip(inputUri));
        resampler.setOutput(outputUri);
        resampler.setOutputResolution(Resolution.RESOLUTION_360P.getWidth(), Resolution.RESOLUTION_360P.getHeight());
        try {
            resampler.start();
        } catch (Throwable e) {
            e.printStackTrace();
        }
@microgomi
Copy link

Hi HardikSuba, I have a same problem, did you resolve this issue?

@HardikSuba
Copy link
Author

Hi microgomi, I am not able to solve it yet.

@gouravd
Copy link

gouravd commented Dec 15, 2015

I do not think this can be solved directly. You might want to mux the video from this converted file and audio from the original file together

@KAPLANDROID
Copy link

Any sample code for mux original audio?

@Berreis
Copy link

Berreis commented Oct 27, 2016

I am also having this problem? Does anyone have an idea on how to solve that?

@KAPLANDROID
Copy link

I added following code to my application. "audioFile" is original video file. You can try this.

import com.coremedia.iso.boxes.Container;
import com.googlecode.mp4parser.authoring.Movie;
import com.googlecode.mp4parser.authoring.builder.DefaultMp4Builder;
import com.googlecode.mp4parser.authoring.container.mp4.MovieCreator;

public Uri mux(String audioFile, String videoFile) {
    String outputFilePath = videoFile.replace(".mp4", "_with_audio.mp4");
    Uri outputFile = Uri.parse(outputFilePath);
    Movie video;

    try {
        video = new MovieCreator().build(videoFile);
    } catch (RuntimeException e) {
        e.printStackTrace();
        return null;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }


    Movie audio;
    try {
        audio = new MovieCreator().build(audioFile);
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    } catch (NullPointerException e) {
        e.printStackTrace();
        return null;
    }

    try {
        // audioFile is my original big videoFile. Index 0 is video, index 1 is audio  in Tracks.
        // if you have a media file with only audio index can be 0. Be carefull
        video.addTrack(audio.getTracks().get(1)); // 
    } catch (Exception e) {
        e.printStackTrace();
    }

    Container out = new DefaultMp4Builder().build(video);

    FileOutputStream fos;
    try {
        fos = new FileOutputStream(outputFile.toString());
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        return null;
    }
    BufferedWritableFileByteChannel byteBufferByteChannel = new BufferedWritableFileByteChannel(fos);
    try {
        out.writeContainer(byteBufferByteChannel);
        byteBufferByteChannel.close();
        fos.close();
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
    return outputFile;
}

@AnswerZhao
Copy link

@KAPLANDROID It doesn't work, how can you do it?

@KAPLANDROID
Copy link

@AnswerZhao what is the error you are getting? Which line does not work?

@Berreis
Copy link

Berreis commented Nov 12, 2016

It worked. Thank you very much @KAPLANDROID

@iamnitesh
Copy link

iamnitesh commented Nov 18, 2016

i am getting error BufferedWritableFileByteChannel cannot be resolved to a type please give me some solution for that.. @KAPLANDROID

@KAPLANDROID
Copy link

KAPLANDROID commented Nov 27, 2016

@iamnitesh this is the class you need

private static class BufferedWritableFileByteChannel implements WritableByteChannel {
       private static final int BUFFER_CAPACITY = 1000000;

       private boolean isOpen = true;
       private final OutputStream outputStream;
       private final ByteBuffer byteBuffer;
       private final byte[] rawBuffer = new byte[BUFFER_CAPACITY];

       private BufferedWritableFileByteChannel(OutputStream outputStream) {
           this.outputStream = outputStream;
           this.byteBuffer = ByteBuffer.wrap(rawBuffer);
       }

       @Override
       public int write(ByteBuffer inputBuffer) throws IOException {
           int inputBytes = inputBuffer.remaining();

           if (inputBytes > byteBuffer.remaining()) {
               dumpToFile();
               byteBuffer.clear();

               if (inputBytes > byteBuffer.remaining()) {
                   throw new BufferOverflowException();
               }
           }

           byteBuffer.put(inputBuffer);

           return inputBytes;
       }

       @Override
       public boolean isOpen() {
           return isOpen;
       }

       @Override
       public void close() throws IOException {
           dumpToFile();
           isOpen = false;
       }

       private void dumpToFile() {
           try {
               outputStream.write(rawBuffer, 0, byteBuffer.position());
           } catch (IOException e) {
               throw new RuntimeException(e);
           }
       }
   }

@syc-pad
Copy link

syc-pad commented May 30, 2017

Big edit :
Sorry, I plugged my brain off : it works pretty well, @KAPLANDROID :) 👍

@northoutRavindra
Copy link

northoutRavindra commented Jun 29, 2017

@KAPLANDROID Where should i add urs code in my application? Sorry i did not get urs solution please help me to short our from this problem..My requirement is that , i want to compress the video only...

Thanks

@northoutRavindra
Copy link

northoutRavindra commented Jun 29, 2017

@HardikSuba @syc-pad @Berreis .. Did u get the solution, please help me to short out from this problem....My requirement is that i want to compress the video only...
Thanks

@KAPLANDROID
Copy link

@northoutRavindra if you use my code sample you will only compress video track. Audio track remains same without any compression. Output files audio track is copy of original input files audio track

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants