javaaudiofileoutputstreamrandom-access

WAV File writing Issue from another wav file


In my program I had to remove certain frames from video and audio so that advertisments are removed. The video is stored in .rgb file and audio is stored in .wav file.

The video is working fine. But when I try and write only a few frames from one wav file to the output wav file it is giving an error while playing.

Though the program is writing it properly and I can play it in Real Player, the audio gives an error called mark/reset not supported and unable to get position error when I run it on the code same code to play wav file that professor has given.

I cant find the mistake in the writing audio function. Here is the function

public void do_audioWrite() throws IOException
{
    long offset=0;
    long offset2=0;
    int audioInfoSize = 2; //audio info is in bytes
    long FRAMESIZE= 352*288*(24/8);
    double MAX_POSSIBLE_FRAMES=AdvRemove.file_len/FRAMESIZE;
    double AUDIO_PER_FRAME = 2000;
    long videoFrames=0;
    byte[] bytes = new byte[4000];
    byte[] head = new byte[44];
    RandomAccessFile raf =  new RandomAccessFile(AdvRemove.afilename,"r");
    OutputStream outputStream = new FileOutputStream(AdvRemove.write_audio);

    raf.seek(0);
    raf.read(head);
    outputStream.write(head);


    for (offset=44; ((offset < raf.length()) && (videoFrames < MAX_POSSIBLE_FRAMES)); offset+=2*AUDIO_PER_FRAME) 
    {

        if(Group1.size()>Group2.size())
        {
            if(Group1.contains((int)videoFrames))
            {

                            raf.seek(offset);
                            raf.read(bytes);
                            outputStream.write(bytes);


            }
        }

        else if(Group1.size()<Group2.size())
        {
            if(Group2.contains((int)videoFrames))
            {
                raf.seek(offset);
                raf.read(bytes);
                outputStream.write(bytes);

            }
        }


        videoFrames++;
    }

}

ABOVE IS MY FUNCTION TO WRITE THE AUDIO.

I only write an audio frame if the corresponding video frame is found in my arrayList called Group1 and Group2

First I took the header of input wav file and write it to the output wav file.

Then I check if corresponding video frame exists. If yes, I write the audio frames corresponding to that video frame.


Solution

  • You need to update the output file header to be correct for the new file you are creating.

    outputStream.write(head);
    

    Here you are simply copy the original files header, but it looks like in your loop you are not always copying a sample from the original file to your stripped file (i.e your output file is going to be shorter than the input file). The wav file format has information in the header regarding the size of the file (see https://ccrma.stanford.edu/courses/422-winter-2014/projects/WaveFormat/ for a break down of the header), you need to update the header to have the correct data length for your new file.