Why not we use original image instead decoded image to P-frame? - mpeg

I'm trying to want to know the P-frame at mpeg.
I have a query about reference image.
Why not we use original image instead decoded image to make P-frame?

I-frame, B-frame and P-frame allows to compress the video.
Indeed, in a video you have a lot of redundant information.
Think about a car moving across the screen: all the pixels in the background do not change from a picture to another, only those around the car are "moving". With the I-B-P frame truck, you give the code of the background and then, you just signalling slight changes (the car moving) through vectors.
This way you have to carry less information than if you have to repeat the entire picture each time.
See also:
Video compression
https://stackoverflow.com/a/24084121/3194340

Related

Resampling HTMLImageElement for animation

An HTMLVideoElement can be resampled in order to get different frames into a texture over time.
For example, as shown at https://developer.mozilla.org/en-US/docs/Web/API/WebGL_API/Tutorial/Animating_textures_in_WebGL
However- when loading an animated gif into a HTMLImageElement, resampling does not show the updated texture. This is true even if the image is mounted on the dom and the different frames show on that copy.
Is there a standard way to display an animated gif in webgl, or must it be somehow rewritten into a spritesheet (or series of textures) at runtime?
GIFs aren't automatically animated with WebGL (or regular canvas for that matter) and there is no standard way of doing this.
Contrary to video elements GIF images will only draw the first frame via drawImage() while drawing video elements will draw current frame. This is in part because we don't really have access to any of the image's frames via API (this also applies to animated PNG files, aka APNG) and animated images will be handled only as an internal process conducted at the discretion of the browser and only when in DOM.
With video elements though we do have access to "frames", that is, time via currentTime so it's sort of implies that we want to deal with what we see or what exist at the current time.
You have to manually animate a GIF image though. This means you have to extract each frames as separate images/buffers first then show them at the rate you chose. The browser won't help you a bit here, but you can do this by parsing the file format manually.
Of course, this can be a bit tedious, but luckily there are people out there that has done all the lifting and hard work. For example gifuct (I have not tested it myself but there are others out there as well) will allow you to extract each frame from a GIF
Then render each frame you got from that into the frame buffer and upload to the GPU at the frame rate you choose.
Or:
pre-process the GIF into a spritesheet as you mention
or load it as an image sequence instead
or convert the GIF to a video (this may even reduce the total size)
And as a shameless plug if you should consider APNG instead: I have made apng-parser which does the same for APNG files.
My recommendation though is to convert the GIF/APNG to a video file which gives the animation capabilities for free, potentially smaller files, can be buffered and streamed for long animations, less code to include and typically a single file to deal with (you may have to provide different video formats for older browsers). Free software such as FFMpeg can help you with the conversion.

How to get perspective coordinates from file for image overlay with ffmpeg?

Is it possible to do something like this purely with ffmpeg?
Lets say we have a text file with the frame by frame coordinates for the 4 corners where the image should go. ffmpeg has a perspective filter, but how would one get ffmpeg to pull the frame coordinates from the text file? I'm guessing with a pipe of sorts?
The perspective filter corrects the input's perspective, it doesn't apply a perspective effect. Applied to an overlay it results in a rectangular overlay with a corrected perspective.
The closest you can get with the already implemented filters is via the frei0r perspective module.
You can write your own filter for ffmpeg or a frei0r module.
Update: using #Mulvya's tip you can use timeline editing with perspective:
perspective=enable='eq(n,0)':x0=...,perspective=enable='eq(n,1)':x0=...
where n is the current frame number.
This will result in an impossibly long command line which may go over the system limit. You're still better writing your own filter.
You can alternatively do one frame at a time with a different command, save the output as an image and re-assemble the video at the end.

Split a movie so that each GIF is under a certain file size

Problem
I want to convert a long movie into a series on animated GIFs.
Each GIF needs to be <5MB.
Is there any way to determine how large a GIF will be while it is being encoded?
Progress So Far
I can split the movie into individual frames:
ffmpeg -i movie.ogv -r 25 frameTemp.%05d.gif
I can then use convert from ImageMagick to create GIFs. However, I can't find a way to determine the likely file size before running the command.
Alternatively, I can split the movie into chunks:
ffmpeg -i movie.ogv -vcodec copy -ss 00:00:00 -t 00:20:00 output1.ogv
But I've no way of knowing if, when I convert the file to a GIF it will be under 5MB.
A 10 second scene with a lot of action may be over 5MB (bad!) and a static scene could be under 5MB (not a problem, but not very efficient).
Ideas
I think that what I want to do is convert the entire movie into a GIF, then find a way to split it by file size.
Looking at ImageMagick, I can split a GIF into frames, but I don't see a way to split it into animated GIFs of a certain size / length.
So, is this possible?
There currently is no "Stop at this filesize" option in avconv that i'm aware of. It can, of course, be hacked together quite quickly, but currently libav project doesn't do quick hacks, so it'll likely appear in ffmpeg first.
In addition to this you are facing a problem of animated gif being a very old format, and thus doing some rather strange things. Let me explain the way it normally works:
You create a series of frames from first to last and put them on top of one another.
You make all the "future" frames invisible, and set to appear at the specific time.
In order to make the size of the file smaller, you look "below" the new frames, and if the previous pixel is the same, you set that particular pixel as opaque.
That third step is the only time compression that is done in the animated gif, without it the file size will be much larger (since every pixel must be saved again and again).
However, if you are unsure when was the last break, you cannot determine if the pixel is the same as the previous "frames". After all, this particular frame can be the very first one in the image.
If the limit of 5MiB is soft enough to allow going a little over it, you probably can put something together that just keeps adding frame after frame, and calculating the final file size right away. As soon as one goes over the limit, just stop and use the next frame as the starting point for the next file.

Animated GIF - avoid storing repeated frames twice

I have an animated gif much like this one, where the cyclic nature of the animation causes some frames to be repeated twice within one full loop of the animation.
(From here)
I am currently storing each frame separately in the gif. Is it possible to only store each repeated frame once, to effectively halve the storage space required?
I am creating my gif in MATLAB using the movie2gif converter, but would be happy with an alternative method for gif creation or a post-processing tool.
EDIT
What I mean by the frame repetition is best explained in the context of this example image. There is a frame shown just as the left-hand ball leaves the row of balls. That exact frame is repeated a few frames later, when the left-hand ball is now on its way back to hit the row of balls again. Because of the ordering of frames, it is currently needed to store this
frame twice.
To clarify what I am looking for: I want a way of saving the gif (or post-processing the gif) such that I can keep the full animation sequence (e.g. of 30 frames), but frames which repeat are soft-linked back to the first showing of them, thus removing the need to store them twice.
Judging from the description of movie2gif and its input arguements, it does not appear to be possible. Furthermore, when reading how gifs work (and LZW) compression I can imagine that it is not even possible to reduce the size of a gif like this.
If you want to save only the images that are minimally required and don't mind building the image before you can see it, then you can just store each image and an indexing vector.
In your case it may be possible to find a way to just save half of the image, and then play it in a cycle: forward-backward-forward ... but I don't know whether this is possible.

Detect frames that have a given image/logo with FFmpeg

I'm trying to split a video by detecting the presence of a marker (an image) in the frames. I've gone over the documentation and I see removelogo but not detectlogo.
Does anyone know how this could be achieved? I know what the logo is and the region it will be on.
I'm thinking I can extract all frames to png's and then analyse them one by one (or n by n) but it might be a lengthy process...
Any pointers?
ffmpeg doesn't have any such ability natively. The delogo filter simply works by taking a rectangular region in its parameters and interpolating that region based on its surroundings. It doesn't care what the region contained previously; it'll fill in the region regardless of what it previously contained.
If you need to detect the presence of a logo, that's a totally different task. You'll need to create it yourself; if you're serious about this, I'd recommend that you start familiarizing yourself with the ffmpeg filter API and get ready to get your hands dirty. If the logo has a distinctive color, that might be a good way to detect it.
Since what you're after is probably going to just be outputting information on which frames contain (or don't contain) the logo, one filter to look at as a model will be the blackframe filter (which searches for all-black frames).
You can write a detect-logo module, Decode the video(YUV 420P FORMAT), feed the raw frame to this module, Do a SAD(Sum of Absolute Difference) on the region where you expect a logo,if SAD is negligible its a match, record the frame number. You can split the videos at these frames.
SAD is done only on Y(luma) frames. To save processing you can scale the video to a lower resolution before decoding it.
I have successfully detect logo using a rpi and coral ai accelerator in conjunction with ffmeg to to extract the jpegs. Crop the image to just the logo then apply to your trained model. Even then you will need to sample a minute or so of video to determine the actual logos identity.

Resources