Lets say I have this video url : http://www.quirksmode.org/html5/videos/big_buck_bunny.mp4
and I need to download just a small playable snippet from this video (e.g. 00:24 - 00:30).
How should I do this in java? And I don't want to download the entire video and then cut it.
I already looked at the network tab of chrome and read the request and response headers but couldn't get much sense of it, except:
"Range:bytes=0-" in the request header
"Content-Length:5510872" in the response header
But I can't just choose any range because it would make the video not playable right?
So how does the browser know which range it should get if I click on 00:24?
assuming you're working with .mp4 containers,
the mp4 file format contains a hierarchical structure of 'boxes' (aka "atoms'). The magic for fast seeking lies in the moov atom which is why when encoding a video for web it's always best to optimize the structure of the file and relocate that to the front so that the browser has access to the metadata as the very first thing it downloads.
moov
Movie box which is the container for all metadata
Each moov has have a mvhd (Movie header box)
It can contains N trak box(es). Each trak box contains media specific meta data information Usually, it will have 2 tracks (video and audio)
More importantly, it contains sample information such as stsd, stts, stsz stsc, stco, etc...
stts - Time-To-Sample (stts) box.
This box contains the time of every frame (in a compact fashion). From here you can find the 'chunk' that contains the frame using the Sample-to-Chunk (stsc) atom. And finally the Chunk offset atom (stco) gives you the byte offset into the file.
Projects like MP4Parser or Xuggler can get you started on processing the MP4 container yourself (have samples on reading the underlying MP4 structure), but it's not a trivial undertaking - sadly there doesn't seem to be a comprehensive MP4 API toolkit for Java
Related
I am maybe asking a dumb question but I would like to be sure as my app is almost finished and I don't want to face some issue with viruses in the future
I have an app written in angular2 and a backend in java.
People can change their profile picture.
From my frontend I encode the picture in base64 and send it with a post to my rest api.
Server check the size of the base64 and reject it if it reached a certain size (but I also have a maxPostSize of 2MB in tomcat by default)
The base64 is then decoded with library net.iharder which transform it in bytearray
http://iharder.sourceforge.net/current/java/base64/
Once it is done I check if the file is a picture (and resize it as well) by creating a BufferedImage with
ImageIO.read(ByteArrayInputStream)
If it does not correspond to an image it returns null So I don't see the risk here as well.
Once it is done I store the picture in my server.
Any profile who consult the profile with picture will receive a base64 encoded image (corresponding to the uploaded one) and it will be displayed in an basic
<img src="myBase64"/>
Only JPG and PNG are allowed
My question is this one: Is there any risk for my server or for the end users if a guy send a file containg a virus? Or am I safe with the ImageIO reader.
Thanks in advance
If you store anything sent to you and send it back unchanged, then anything can happen. Just because ImageIO can read the image, doesn't mean that there's not something compromising in there.
However, if you resize the image, and use that, then there's pretty much no chance of anything surviving that as you're creating a brand new image from raw image bytes. JPG and (I guess) PNG files can contain meta data that's not part of the image, and those can potentially be vectors for exploits. But by creating a new image from the raw image data, you implicitly strip all of that.
An online link gives me back a video in my browser, that is then streamed. In my browser, I don't need to download the whole video to visualize it - I can skip to a certain part, visualize that part, and even stop watching before the end.
How can I, from the very same link, start to stream from a specific time onwards until another specific time, and each time save every frame I received to a video file?
I do know that I need to decode the data I get.
I also know that I need to somehow access the metadata of the video file.
However, I don't know how to deal with it in practice.
I tried out JCodec, but could not find how to deal with internet streams instead of opening files.
How can I properly stream parts of an online video, frame-by-frame, while also having access to metadata such as total video length, resolution, and FPS?
To answer the first parts of your question:
Trim videos with start offset and either end offset or duration (in seconds, percentages, etc.), e.g. http://res.cloudinary.com/demo/video/upload/so_6.5,eo_10/dog.mp4
Grab frames at desired timestamps along the original video, e.g. http://res.cloudinary.com/demo/video/upload/so_8.5/dog.jpg to save a JPEG-format frame at 8.5 seconds from the start of the video.
I've a stream with contains a audio/video stream. The video encoding is H.264 AVC and the audio encoding is G.711 ยต-law (PCMU). (I have no control over the output format.)
When I try to display the video into a VideoView frame, the device says that it cannot play the video. I guess that's because Android doesn't support the aforementioned audio encoding.
Is there a way to somehow display the selected stream in the VideoView?
Can I just use a Java code snippet to 'compute' the codec? I've seen the class android.net.rtp.AudioCodec, which contains a field PCMU, and I can imagine that class would be useful.
Or do I have to insert a library or some native code (FFmpeg) into my application and use that? If yes, how?
How to fix it?
If you want to manipulate a stream before you display it, you are unfortunately getting into tricky territory on an Android device - if you do have any possibility of doing the conversion on the serve side it will likely by much easier (and you should also usually have more 'horsepower' on the server side).
Assuming that you do need to convert on the server side, then a technique you can use is to stream the video from the server to your app, convert it as needed and then 'stream' from a localhost server in your app to a VideoView in your app. Roughly the steps are:
Stream the encrypted file from the server as usual, 'chunk by chunk'
On your Android device, read from the stream and convert each chunk as it is received
Using a localhost http server on your Android device, now 'serve' the converted chunks to the MediaPlayer (the media player should be set up to use a URL pointing at your localhost http server)
An example of this approach, I believe, is LibMedia: sure: http://libeasy.alwaysdata.net (note, this is not a free library, AFAIK).
For the actual conversion you can use ffmpeg as you suggest - there are a number of ffmpeg wrapper for Android projects that you can either use or look at to design your own. Some examples:
http://hiteshsondhi88.github.io/ffmpeg-android-java/
https://github.com/jhotovy/android-ffmpeg
Take a look at the note about libffmpeginvoke in the second link in particular if you are planning to write your won wrapper.
One thing to be aware of is that compressions techniques, in particular video compression, often use an approach where one packet or frame is compressed relative to the frames before it (and sometimes even the frames after it). For example the first frame might be a 'key frame', the next five frames might just contain data to explain how they differ from the key frame, and the the seventh frame might be another key frame and so on. This means you generally want a 'chunk' which has all the required key frames if you are converting the chunk from one format to another.
I don't think you will have this problem converting from PCM to AAC audio, but it useful to be aware of the concept anyway.
I had downloaded a .flv video of large size using Orbit downloader which cuts the file into eight parts and then joins them back. After downloading , I have found that the video is not running and stops after some time. I want to extract all the video and save it in another file using java program.Can anyone help me , please ???
You need to understand FLV file format for this. Check out http://www.adobe.com/devnet/f4v.html.
Typically flv file contains header fields and some of those fields specify track length, if the file stops in between , your header fields are not storing the right values.
I am trying to implement a Java servlet that runs on Tomcat, capable of streaming multiple FLV files to client browsers having JWPlayer. The catch is I have to stream multiple files one at a time and sometimes start streaming from the middle of the first clip and I need JWPlayer to think that the file duration is the duration of all the clips combined.
My servlet would work well if I merged all of the clips to one single FLV file, injected the metadata (using yamdi) and then streamed it. But this can be pretty time consuming. I've tried sending the player the meta information for the file that I stream from the middle first and then go ahead and stream it from the middle but this doesn't seem to work. I've tried fiddling with the duration parameter in the metadata to no avail.
I think that this is because I'm skipping tags when i start to stream from the middle of the clip. Would it be humanly possible to construct tags while processing the byte stream before the servlet sends it out?
You don't need the meta data, other than the initial FLV header and individual frame descriptions. As long as your FLV frames are correctly and atomically started and stopped, what you are doing seems very possible to me. (I had considered doing something similar, having already written an FLV parser.) Be sure not to send a Length header. ;) Two things might make all of this much easier to accomplish:
ensure you've encoded your video with smallish key frame interval. You won't be able to jump between clips at any finer of a resolution than this. Anything less that 1s is probably going to be problematic, much higher video rates.
pre-parse all your video files into segments. When the servlet is called,
send the FLV header
read and write whole segment files to the client
1- to switch videos, move to another group of segment files
Example that assumes you want to send each original file from the start:
send(FLV_HEADER)
i = 0
while(send file 1 condition == true)
send(file-1-segment i++)
i = 0
while(send file 2 condition == true)
send(file-2-segment i++)
(Alternatively you could map some indexes and use them to read frames from the middle of a file. Been there, done that.)