I currently am accessing a streaming h264 file and want to save it off for the ability to slice frames. However, I'm having issues saving/opening the .flv file
When pointing to the URL in the address bar - I am told it's an x-flv file.
I then attempt to do the following to save a chunk of the stream.
URL url = new URL("http://foo.bar.com/foo/bar");
HttpURLConnection conn = (HttpURLConnection) url
.openConnection(proxy);
conn.setRequestMethod("GET");
File f = new File("C:\\tmpArea\\tmp.flv");
FileWriter fr = new FileWriter(f);
bw = new BufferedWriter(fr);
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
String output = "";
int i = 0;
while (((output = br.readLine()) != null) && i < 100000) {
bw.write(output);
i++;
}
Upon doing this I've attempted to open the file in VLC Media Player and am told:
No suitable decoder module: VLC does not support the audio or video format "undf".
Unfortunately there is no way for you to fix this.
I then thought, well maybe it's not really an FLV file and that's on me. So I used a run of the mill hex-editor. Opening up the file in the HexEditor gives me the following information:
FLV
onMetaData
duration
width
height
videodatarate
framerate
videocodecid
audiodatarate
audiosamplerate
audiosamplesize
stereo
audiocodecid
encoder
Lavf52.10.6.0
filesize....
Is there a different way I should be trying to save off this data? Is there a conversion/codec issue I'm not seeing?
You are using a Reader and Writer, which are intended to read bytes and convert them to characters, to read a binary file that consists of bytes. The conversion from bytes to characters will corrupt the data. You should be using InputStream and OutputStream instead.
Related
This question already has an answer here:
How to Merge Multiple MP4 Videos files in single file
(1 answer)
Closed 3 years ago.
Could someone please tell me what is wrong in the below code, I'm trying to merge two different video URLs to same file, (both videos have the same size 1024x720)
String url1 = "https://test.com/vid1";
String url2 = "https://test.com/vid2";
FileOutputStream out = new FileOutputStream(new File("test.mp4"));
writeToFile(url1, out);
writeToFile(url2, out);
out.close();
//Even tried the below way of first saving one file and then opening the same file to append the stream data
/*
FileOutputStream out = new FileOutputStream(new File("test.mp4"));
writeToFile(url1, out);
out.close();
out = new FileOutputStream(new File("test.mp4"), true);
writeToFile(url2, out);
out.close();
*/
void writeToFile(String url, FileOutputStream out) {
HttpsURLConnection con = (HttpsURLConnection) new URL(url).openConnection();
con.setRequestMethod("GET");
BufferedInputStream bis = new BufferedInputStream(con.getInputStream());
int count;
byte buf[] = new byte[20480];
while((count = bis.read(buf, 0, 20480)) != -1)
out.write(buf, 0, count);
bis.close();
con.disconnect();
}
I have tried to save the file using the above two methods but both create only one video file i.e., the second video is not appended (i'm able to save both files if given different names)
The problem is replacing the content of file and not concat.
the function FileOutputStream(File file, boolean append) use second parameter for this purpose. use this method with true value for the second parameter
To concatenate two videos you need special software. ffmpeg is one:
ffmpeg -i vid-1.mp4 -i vid-2.mp4 -filter_complex "[0:v:0][0:a:0][1:v:0][1:a:0]concat=n=2:v=1:a=1[v][a]" -map "[v]" -map "[a]" all.mp4
If you want to play the combined video. If you only need to store the info, your usual way should work.
I have decrypted data in bytearrayoutputstream. I want to read the data in each line(not sure if that is possible).Could any one guide how I can do that.
The main requirement is to read a encrypted file , decrypt and read the data without writing into the disk. I have already covered encrypt and decrypt part but unable to read the data without writing into disk.Some suggested to use bytearrayoutputStream so stuck now.
ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream(inputBytes.length);
byteArrayOutputStream.write(outputBytes);
if i simply print the variable it give me all the data at once as below.
SQlServer,"connection string","user name","password"
Oracle,"connection string","user name","password"
I am trying to read the data line wise so i can match the servername and fetch the user name and other details.
To read a byte[] you can use
ByteArrayInputStream in = new ByteArrayInputStream(outputBytes);
and to read this as lines of text you can use
BufferedReader br = new BufferedReader(new InputStreamReader(in));
for (String line; (line = br.readLine()) != null; ) {
// do something with the line
}
I am trying to read and write metadata to/from an mp3 file. I can read the data, but I cannot figure out how to write it.
I use the following code to get file access:
FileConnection file = (FileConnection) Connector.open("file:///store/home/user/music/song.mp3");
if(file.exists())
java.io.InputStream inputStream = file.openInputStream();
Later, I read the data using the following code:
buffer = new byte[length]; // length is predetermined earlier
if (inputStream.read(buffer, 0, length) == length)
String info = new String((buffer));
How do I write data (bytes) to the a designated location in the file? I am unsure of both the IO declarations and the specific code required to output my bytes.
To write a file on Blackberry use the following code:
FileConnection fconn = null;
OutputStream out = null;
try {
fconn = (FileConnection) Connector.open(yourFileNameAndPath,Connector.READ_WRITE);
}
fconn.create();
out = fconn.openOutputStream();
out.write(yourDataBytes);
out.flush();
fconn.close();
I have a simple question. I'm trying to upload a file to my ftp server in Java.
I have a file on my computer, and I want to make a copy of that file and upload it. I tried manually writing each byte of the file to the output stream, but that doesn't work for complicated files, like zip files or pdf files.
File file = some file on my computer;
String name = file.getName();
URL url = new URL("ftp://user:password#domain.com/" + name +";type=i");
URLConnection urlc = url.openConnection();
OutputStream os = urlc.getOutputStream();
//then what do I do?
Just for kicks, here is what I tried to do:
OutputStream os = urlc.getOutputStream();
BufferedReader br = new BufferedReader(new FileReader(file));
String line = br.readLine();
while(line != null && (!line.equals(""))) {
os.write(line.getBytes());
os.write("\n".getBytes());
line = br.readLine();
}
os.close();
For example, when I do this with a pdf and then try and open the pdf that I run with this program, it says an error occurred when trying to open the pdf. I'm guessing because I am writing a "\n" to the file? How do I copy the file without doing this?
Do not use any of the Reader or Writer classes when you're trying to copy the byte-for-byte exact contents of a binary file. Use these only for plain text! Instead, use the InputStream and OutputStream classes; they do not interpret the data at all, while the Reader and Writer classes interpret the data as characters. For example
OutputStream os = urlc.getOutputStream();
FileInputStreamReader fis = new FileInputStream(file);
byte[] buffer = new byte[1000];
int count = 0;
while((count = fis.read(buffer)) > 0) {
os.write(buffer, 0, count);
}
Whether your URLConnection usage is correct here, I don't know; using Apache Commons FTP (as suggested elsewhere) would be an excellent idea. Regardless, this would be the way to read the file.
Use a BufferedInputStream to read and BufferedOutputStream to write. Take a look at this post: http://www.ajaxapp.com/2009/02/21/a-simple-java-ftp-connection-file-download-and-upload/
InputStream is = new FileInputStream(localfilename);
BufferedInputStream bis = new BufferedInputStream(is);
OutputStream os =m_client.getOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(os);
byte[] buffer = new byte[1024];
int readCount;
while( (readCount = bis.read(buffer)) > 0) {
bos.write(buffer, 0, readCount);
}
bos.close();
FTP usually opens another connection for data transfer.
So I am not convinced that this approach with URLConnection is going
to work.
I highly recommend that you use specialized ftp client. Apache commons
may have one.
Check this out
http://commons.apache.org/net/api/org/apache/commons/net/ftp/FTPClient.html
In Google App Engine, I tried reading a .txt file from a URL. Because the maximum allowed size is 1MB and the file is slightly larger, I'm using an alternative method described here.
So, what I'm trying to do is this:
FetchOptions fo = FetchOptions.Builder.allowTruncate().doNotFollowRedirects();
HTTPRequest request = new HTTPRequest(url,HTTPMethod.GET,FetchOptions.Builder.allowTruncate());
URLFetchService service = URLFetchServiceFactory.getURLFetchService();
HTTPResponse response = service.fetch(request);
My question is now, how can I read this response line by line? I'm trying to process each line which should be possible somehow as the source file is a simple text file.
I can get a byte[] with
byte[] content = response.getContent();
but I'm struggling with the further processing of it.
Or, can I do something completely different to achieve the same thing ?
I'm trying to read it line by line because I don't need all the lines. Processing would be much easier than to put everything in one large string.
You can try:
ByteArrayInputStream bais = new ByteArrayInputStream(content);
BufferedReader reader = new BufferedReader(new InputStreamReader(bais, "utf-8"));
String line = null;
while ((line = reader.readLine()) != null) {
...
}
Alternatively, you can use IOUtils and call IOUtils.lineIterator(reader) (where reader is the InputStreamReader)