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.
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.
Related
I am using TarsosDSP library to apply a low pass filter on a wav file. Here is the code.
private void eq2(File file) throws FileNotFoundException {
new AndroidFFMPEGLocator(this);
InputStream inputStream = new FileInputStream(file);
TarsosDSPAudioFormat format = new TarsosDSPAudioFormat(16000,16,2,true,false);
AudioDispatcher adp = new AudioDispatcher(new UniversalAudioInputStream(inputStream,format),2048,0);
adp.addAudioProcessor(new LowPassFS(100,16000));
RandomAccessFile raf = null;
raf = new RandomAccessFile(Environment.getExternalStorageDirectory()+ "/cibiodLogs/audioFiltered1.wav", "rw");
adp.addAudioProcessor(new WriterProcessor(format,raf));
adp.run();
Thread audioThread = new Thread(adp, "Audio Thread");
audioThread.start();
}
It gives output but the output is choppy and not even filtered.
Here take a look at the original wav file and the output wav file.
Original
Filtered
I have tried different buffer sizes from 2 - 4096 but every time either the output is choppy or the audio is not filtered. Can someone point me what might be going wrong here.
This issue is solved now! The TarsosDSP does not support dual-channel audio processing so the algorithm was getting confused with two channels of audio and thus giving the choppy results.
I have a file movie.mp4 that I wish to create a preview of and play in a VideoView. For the purpose of this question let's say movie.mp4 is 30 megabytes.
I want to extract the first 10 megabytes of video from movie.mp4 and save it in a temporary file. I will feed this file into the VideoView and play it.
This is what I've done so far:
// Get movie file.
File f = getMovieFile("movie.mp4");
// Create temporary file.
File t = File.createTempFile("preview", "mp4");
t.deleteOnExit();
// Create input stream.
InputStream is = new FileInputStream(f);
BufferedInputStream bis = new BufferedInputStream(is, 2048);
// Create output stream to temp file.
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(t));
byte[] buffer = new byte[16384];
int totalRead = 0;
int bytesRead;
int maxBytes = (int) Math.pow(2, 20) * 10; // 10 megabytes
// Write data from input to output.
while ((bytesRead = bis.read(buffer)) != -1 && totalRead < maxBytes) {
bos.write(buffer, 0, bytesRead);
bos.flush();
totalRead += bytesRead;
}
bos.close();
// Play the preview.
mVideoView.setVideoPath(t.getAbsolutePath());
mVideoView.start();
When I run this bit of code Android complains and tells me that the file is unplayable. If I remove the 10 megabyte restriction and let the loop run its course, the VideoView will play the file just fine.
I'm guessing that when the preview is created and only data from one part of the file is copied, some important meta-data from the original mp4 file is missing in the new one. This is just a guess though.
The problem could be in your video,
a partial mp4 must have metadata at the beginning of the file.
While there are tons of articles about encoding MPEG 4 videos with FFmpeg, most of them fail to warn you about something: most video players will not start the playback until the metadata is available (meaning if that data is placed at the back-end of file, as done by some encoders, the whole video must be downloaded first to reach the metadata & playback begins).
This example use ffmpeg for create a mp4 faststart video:
ffmpeg.exe -i INPUT_FILE_NAME_HERE -c:v libx264 -profile:v baseline
-preset slow -b:v 800k -c:a libvo_aacenc -movflags faststart OUTPUT_FILE_NAME.mp4
i am working on my final year project in JAVA
1)hiding text in a image
2)image in a image
3)text in a audio file (WAVE)
i have successfully completed 1) and 2) and have attached the source code if anybody may need it.
i am having trouble in the 3rd one i.e hiding data in a audio file .
I create a audioinputstream out of a wave file and read it's data into a byte array
but many things are not clear,while reading i'm guessing the 1st 44 bytes are the header bytes?(since the file is of WAVE format) or the header is not copied at all.
The Problem is ....
at the time of decoding again i have to read the data from the newly created audio file in a byte array. And i'm not able to locate the bytes where i have hidden data.
Can anybody tell me what exactly happens when we read data into a byte array from a audioinputstream , i mean what actually gets read into the byte array?
File fileIn = new File("C:\\Users\\Rahul\\Desktop\\pro\\Don't Stay.wav");
AudioInputStream audioInputStream =
AudioSystem.getAudioInputStream(fileIn);
int avail= audioInputStream.available();
System.out.println("bytes available " +avail);
System.out.println(audioInputStream.markSupported());
int bytesPerFrame =
audioInputStream.getFormat().getFrameSize();
// Set an arbitrary buffer size of 1024 frames.
int numBytes = 1024 * bytesPerFrame;
byte[] audioBytes = new byte[numBytes];
audioInputStream.read(audioBytes);
byte btext[]=Stego_text("good morning!");
byte bcoded[]=steg.encoding(audioBytes,btext,0);
byte[] stg= a.decode_text(audioBytes);
String obtain= new String(stg);
System.out.println(">>>"+ obtain); //the hidden message gets successfully displayed here
try {
//
AudioSystem.write(audioInputStream, Type.WAVE, new File("C:\\Users\\Rahul\\Desktop\\pro\\Don't Stay_restored.wav"));
} catch (Exception e) {
e.printStackTrace();
}
byte[] audioBytesNew = new byte[numBytes];
audioInputStream.read(audioBytesNew);
byte[] stg1= a.decode_text(audioBytesNew);
String obtain1= new String(stg1);
System.out.println(">>>"+ obtain1); //the hidden message does not get displayed
if i decode the byte array just after editing , then it works fine and displays the hidden message, but after again creating a byte array and reading into it audioinputsream data and then decoding that byte array .. it does not work. i wonder WHY? please help me.
The first 44 bytes are indeed the header of the WAV (see https://ccrma.stanford.edu/courses/422/projects/WaveFormat/)
If you open the file in an HEX editor, you'll see what it looks like (http://imgur.com/iliA40R)
If you compare the data in the file and the data read by your InputStream, it matches.
You should close and re-open your stream if you need to read the file again.
If you can mark the file, you could mark it before reading, and call reset() after the first read is done.
Here is my code that concatenates four wav files and produces wavAppended.wav. This concatenated file nicely plays in Windows Media Player.
But through the PlaySound class, only the one.wav can be heard.
Can anyone help?
class PlaySound extends Object implements LineListener
{
File soundFile;
JDialog playingDialog;
Clip clip;
public void PlaySnd(String s) throws Exception
{
JFileChooser chooser = new JFileChooser();
soundFile = new File(s);
Line.Info linfo = new Line.Info(Clip.class);
Line line = AudioSystem.getLine(linfo);
clip = (Clip) line;
clip.addLineListener(this);
AudioInputStream ais = AudioSystem.getAudioInputStream(soundFile);
clip.open(ais);
clip.start();
}
public void update(LineEvent le)
{
LineEvent.Type type = le.getType();
playingDialog.setVisible(false);
clip.stop();
clip.close();
}
}
public class Main
{
public static void main(String[] args)
{
int i;
String wavFile[] = new String[4];
wavFile[0] = "D://one.wav";
wavFile[1] = "D://two.wav";
wavFile[2] = "D://three.wav";
wavFile[3] = "D://space.au";
AudioInputStream appendedFiles;
try
{
AudioInputStream clip0=AudioSystem.getAudioInputStream(new File(wavFile[0]));
AudioInputStream clip1=AudioSystem.getAudioInputStream(new File(wavFile[1]));
AudioInputStream clip3;
for (i=0;i<4;i++)
{
appendedFiles = new AudioInputStream(
new SequenceInputStream(clip0, clip1),
clip0.getFormat(),
clip0.getFrameLength() + clip1.getFrameLength());
AudioSystem.write(appendedFiles, AudioFileFormat.Type.WAVE, new File("D:\\wavAppended.wav"));
clip3 = AudioSystem.getAudioInputStream(new File("D:\\wavAppended.wav"));
clip0=clip3;
clip1 = AudioSystem.getAudioInputStream(new File(wavFile[i+2]));
}
PlaySound p = new PlaySound();
p.PlaySnd("D://wavAppended.wav");
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
WAV files don't work that way -- you can't just throw multiple files together (same as you can't concatenate JPEG images, for instance), as there's a header on the data, and there are multiple different formats the data may be in. I'm surprised that the file loads at all.
To get you started with the WAV processing you may have a look at my small project. It can copy and paste WAV files together based on an time index file. The project should contain all the Java WAV processing you need (using javax.sound.sampled). The Butcher implementation and Composer contain the actual processing.
The idea is simple: take input audio files and create a index of words
contained in these files. The index entry is the word, start time and
end time. When a new sentence is created it will be stitched together
with single words taken from the index.
The AudioInputStream is the main class to interact with the Java Sound
API. You read audio data from it. If you create audio data you do this
by creating a AudioInputStream the AudioSystem can read from. The
actual encoding is done by the AudioSystem implementation depending on
the output audio format.
The Butcher class is the one concerned with audio files. It can read
and write audio files and create AudioInputStreams from an input byte
array. The other interesting think the Butcher can is cutting samples
from a AudioInputStream. The AudioInputStream consists of frames that
represent the samples of the PCM signal. Frames have a length of
multiple bytes. To cut a valid range of frames from the
AudioInputStream one has to take the frame size into account. The
start and end time in milliseconds have to be translated to start byte
and end bytes of the start frame and end frame. (The start and end
data is stored as timestamps to keep them independent from the
underlying encoding of the file used.)
The Composer creates the output file. For a given sentence it takes
the audio data for each word from the input files, concatenates the
audio data and writes the result to disk.
In the end you'll need some understanding of the PCM and the WAV format. The Java sound API does not abstract that away.
In above given example you need to use the SequenceInputStream then it will work fine. please find my code below to join two files.
import java.io.File;
import java.io.IOException;
import java.io.SequenceInputStream;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
public class JoinWav{
public static void main(String... args) throws Exception{
String wav_1 = "1497434542598100215.wav";
String wav_2 = "104860397153760.wav";
AudioInputStream stream_1 = AudioSystem.getAudioInputStream(new File(wav_1));
AudioInputStream stream_2 = AudioSystem.getAudioInputStream(new File(wav_2));
System.out.println("Info : Format ["+stream_1.getFormat()+"] Frame Length ["+stream_1.getFrameLength()+"]");
AudioInputStream stream_join = new AudioInputStream(new SequenceInputStream(stream_1,stream_2),stream_1.getFormat(),stream_1.getFrameLength()+stream_2.getFrameLength());
AudioSystem.write(stream_join,AudioFileFormat.Type.WAVE,new File("join.wav"));
}
}
I would like to create a simple program (in Java) which edits text files - particularly one which performs inserting arbitrary pieces of text at random positions in a text file. This feature is part of a larger program I am currently writing.
Reading the description about java.util.RandomAccessFile, it appears that any write operations performed in the middle of a file would actually overwrite the exiting content. This is a side-effect which I would like to avoid (if possible).
Is there a simple way to achieve this?
Thanks in advance.
Okay, this question is pretty old, but FileChannels exist since Java 1.4 and I don't know why they aren't mentioned anywhere when dealing with the problem of replacing or inserting content in files. FileChannels are fast, use them.
Here's an example (ignoring exceptions and some other stuff):
public void insert(String filename, long offset, byte[] content) {
RandomAccessFile r = new RandomAccessFile(new File(filename), "rw");
RandomAccessFile rtemp = new RandomAccessFile(new File(filename + "~"), "rw");
long fileSize = r.length();
FileChannel sourceChannel = r.getChannel();
FileChannel targetChannel = rtemp.getChannel();
sourceChannel.transferTo(offset, (fileSize - offset), targetChannel);
sourceChannel.truncate(offset);
r.seek(offset);
r.write(content);
long newOffset = r.getFilePointer();
targetChannel.position(0L);
sourceChannel.transferFrom(targetChannel, newOffset, (fileSize - offset));
sourceChannel.close();
targetChannel.close();
}
Well, no, I don't believe there is a way to avoid overwriting existing content with a single, standard Java IO API call.
If the files are not too large, just read the entire file into an ArrayList (an entry per line) and either rewrite entries or insert new entries for new lines.
Then overwrite the existing file with new content, or move the existing file to a backup and write a new file.
Depending on how sophisticated the edits need to be, your data structure may need to change.
Another method would be to read characters from the existing file while writing to the edited file and edit the stream as it is read.
If Java has a way to memory map files, then what you can do is extend the file to its new length, map the file, memmove all the bytes down to the end to make a hole and write the new data into the hole.
This works in C. Never tried it in Java.
Another way I just thought of to do the same but with random file access.
Seek to the end - 1 MB
Read 1 MB
Write that to original position + gap size.
Repeat for each previous 1 MB working toward the beginning of the file.
Stop when you reach the desired gap position.
Use a larger buffer size for faster performance.
You can use following code:
BufferedReader reader = null;
BufferedWriter writer = null;
ArrayList list = new ArrayList();
try {
reader = new BufferedReader(new FileReader(fileName));
String tmp;
while ((tmp = reader.readLine()) != null)
list.add(tmp);
OUtil.closeReader(reader);
list.add(0, "Start Text");
list.add("End Text");
writer = new BufferedWriter(new FileWriter(fileName));
for (int i = 0; i < list.size(); i++)
writer.write(list.get(i) + "\r\n");
} catch (Exception e) {
e.printStackTrace();
} finally {
OUtil.closeReader(reader);
OUtil.closeWriter(writer);
}
I don't know if there's a handy way to do it straight otherwise than
read the beginning of the file and write it to target
write your new text to target
read the rest of the file and write it to target.
About the target : You can construct the new contents of the file in memory and then overwrite the old content of the file if the files handled aren't so big. Or you can write the result to a temporary file.
The thing would probably be easiest to do with streams, RandomAccessFile doesn't seem to be meant for inserting in the middle (afaik). Check the tutorial if you need.
I believe the only way to insert text into an existing text file is to read the original file and write the content in a temporary file with the new text inserted. Then erase the original file and rename the temporary file to the original name.
This example is focused on inserted a single line into an existing file, but still maybe of use to you.
If it is a text file,,,,Read the existing file in StringBuffer and append the new content in the same StringBuffer now u can write the SrtingBuffer on file. so now the file contains both the existing and new text.
As #xor_eq answer's edit queue is full, here in a new answer a more documented and slightly improved version of his:
public static void insert(String filename, long offset, byte[] content) throws IOException {
File temp = Files.createTempFile("insertTempFile", ".temp").toFile(); // Create a temporary file to save content to
try (RandomAccessFile r = new RandomAccessFile(new File(filename), "rw"); // Open file for read & write
RandomAccessFile rtemp = new RandomAccessFile(temp, "rw"); // Open temporary file for read & write
FileChannel sourceChannel = r.getChannel(); // Channel of file
FileChannel targetChannel = rtemp.getChannel()) { // Channel of temporary file
long fileSize = r.length();
sourceChannel.transferTo(offset, (fileSize - offset), targetChannel); // Copy content after insert index to
// temporary file
sourceChannel.truncate(offset); // Remove content past insert index from file
r.seek(offset); // Goto back of file (now insert index)
r.write(content); // Write new content
long newOffset = r.getFilePointer(); // The current offset
targetChannel.position(0L); // Goto start of temporary file
sourceChannel.transferFrom(targetChannel, newOffset, (fileSize - offset)); // Copy all content of temporary
// to end of file
}
Files.delete(temp.toPath()); // Delete the temporary file as not needed anymore
}