I would like to insert still images into a mp4 sequence by PHP on server side, where the position of the placed image would move, for example I'd place an image into a photo frame with chroma-key color (or specified coordinates) in a room and the position of this photo frame would move in the video (if possible..). Much similar to Facebook's "Friends Day" Video. See this example link.
Is there any class or function that would allow me to do so?
Check-ed out this previous question but seemed inconclusive? Inserting an image inside a video at particular position via PHP and a video extension
Thanks in advance for your help!
You're gunna need more than PHP. You need to understand some things about pixel programming and its sub-category : computer vision.
Before I show you the reality check, let's assume the task is really as simple as to just "...Insert still images into a mp4 sequence by PHP". What would you need?
Try PHP :
If you prepare your input footage to have something like a green colour box where image should be inserted then you can consider this PHP method for replacing the green pixels with user photo.
You will have to do this for every single frame. To get video broken down to single frame PNGs try a tool like FFmpeg...
Try FFmpeg :
First have FFmpeg installed on your server (there's even a FFmpeg to PHP connection, ie: send commands and receive the result). This will be used to extract from source, overlay images and later encode the new output video. You can see this answer for overlaying images at specific positions.
In the snippet below, two input images are specified (vid_frame1.png is a video grab which needs an image to be inserted, over_image1.png is the to-be-overlaid image). overlay=75:200 means position overlay image at 75px across (x) and 200px down (y).
ffmpeg -i vid_frame1.png -i over_image1.png -filter_complex "[0:v][1:v] overlay=75:200" new_frame1.png
As you can see, you have to specify the position so in a moving video (where placement position will change over time) you need to keep track of expected image position in each frame. In a 24 FPS video that means a possible 24 unique positions per second which might get tedious. You would run the above command 24 times, each time changing the overlayposition.
note : If you install FFmpeg and use it with PHP then you could also do a stdinput / stdoutput process. This means you can output directly to video file instead of outputting "edited frame" PNGs. The video is generated as your program runs and when it's finish, there's a complete video file. This is too long to explain but you can information about it.
The reality check...
What you're talking about is called "dynamic video". You need a system in place to handle tasks like pixel tracking, perspective distortion and motion blur amongst others. This system would be a server based app (made using Java? or C#?). Here is one example of a dynamic editing tool. Also try coding something like this distortion tool (found here) so if we take that green block idea, now you simply find the four corners of green block in source video frame and position those distort points to each corner and you have persective distortion etc. For motion blur via PHP (I never tried that but) there is Google search...
Related
The effect I would like to generate is exactly like the example in
this StackOverflow thread: (Related Question)
1.Resize image
2.Keep proportion
3.Add or Fill none-image areas with white background
Here are three examples of this process below:
1. If original is square no matter 640*640 or 1024*1024, just resize it to target dimension e.g. 480*480 will work.
If the original input is vertical rectangular,the output should not be cropped
and still be centerlized as below (the red dash edge marker is just make it easier to see the background is white and the proportion is kept after image resized to 480*480 px)
if the original input is horizontal rectangular, output like below, white background, keeps proportion and image uncroped , thus without losing anything after the image processing.
So after I've clarified such a question,
I would like to know:
Is there a general name of such Custom Image Resize mentioned above?
would like to know how to achieve it using php image library Imagine?
is it doable using php-imagine, or have to switch to another library, e.g imagemagick?
P.S. If you would like to achieve the effect either in image-resizing or video resizing, you can use below FFMPEG single-line command.
(thanks to #Mulvya, yes below code applies both to videos and image formats)
[run below built-in code in ffmpeg console to achieve the mentioned resize effect]
[video resize]
ffmpeg -i "[raw video url or videofile location.mp4]" -vf "scale=iw*min(480/iw\,480/ih):ih*min(480/iw\,480/ih),pad=480:480:(480-iw)/2:(480-ih)/2:color=white" [save_path_and_filename].mp4
[image resize]
ffmpeg -i "[raw image url or imagefile location.jpg]" -vf "scale=iw*min(480/iw\,480/ih):ih*min(480/iw\,480/ih),pad=480:480:(480-iw)/2:(480-ih)/2:color=white" [save_path_and_filename].jpg
Sorry i don'T have full answer for you and it's too long for a comment, but this can be done natively using some maths and php's http://php.net/manual/en/ref.image.php
Check in the area of imagecopyresize() imageecreate() imagescale and so on. It a pretty complex lib so you will have to do some try and mistake to get where you want to go.
From experience you can:
create an empty image, imagecreatetruecolor()
change background color to white
take a scetion of an image and paste it in the new created image using imagecopyresample() imagecopyresize()
export this image ans gif, png, jpg. imagepng(), imagejpg().
add watermark
and ALOT more.
Sorry i don'T have a more complete answer but depending on your need it can take a few mitues/hours to make it perfect. have fun.
On top of it, if you know ffmpeg, you can make both work together and make awesome media outputs. Cheers to you
Overview:
I am working on a video creation project. The technology I am using are: imageMagick, php, ffmpeg.
Current Status:
Currently the project is able to create videos using images and texts and few basic transitions. The way I am doing it is using imagemagick to create gif using input images(with transition effects in them) and then converting all gifs to videos and atlast concatenating the video together.
Next Move (My question):
I am now set to take it to the next level. So, what I am having is a video(1920x1080) with some white frames(1280x720) that keeps shifting in each frame. I want to replace those white frames appearing in some frames of the video with some images(1280x720) that I wish to use. Please see the image here and you will get an idea: These are just two frames from my video. If you can see carefully the images are shifting(white space is not constant).
Expectation:
So, I want to fill those white space with one of my own image. If the case would have been for only one frame I could have used ffmpeg to overlay image on the exact width and height. But here the white space is not fixed and keeps shifting in all the frames and there are a lot of frames. So, I am looking for something like opencv or some other technology that can be used for object detection in a video or in a set of frames and replace the detected area with some other image.
I just need a kick. So, if anyone has already worked on something like this just suggest me what technology can I use. Thanks in advance.
It all depends on exactly what you can assume :
If you can safely assume that your rectangle's boundary is never occluded (hidden) somehow, you can try finding the edges in your image (like OpenCV's Canny edge) and then look for rectangular shape (corners forming a warped rectangle, or the very popular Hough Lines).
If the rectangle you're looking for is always white, you can threshold the image in a colorspace like HSV to look for maximum value (the V in HSV ~ brightness) then rectangular shape search in a binary image.
If your corners are occluded sometimes you'll have to do some tweaking with your image, like morphological operations ("grow and contract" binary thresholded image), then Hough Lines could do the trick.
Note that this answer assumes that once you know where the rectangle is, "you're done", and you just have to overwrite the rectangle with custom content.
I also do not check for any time-continuity : you video frame might jump around based on the frame-by-frame appearance of rectangle. You'd have to include some knowledge about previous positions.
I am working on a project in which a user can upload an image and then after uploading the image, a video is generated and then displayed, containing the image which was uploaded as some sort of texture/mask. (Like they do in Elf Yourself).
The original video should contain some sort of placeholder for the image to allow it to move around and be transformed on a matrix.
I'm not really sure where to start off or how I should even call such a merging/blending process in the first place.
A few requirements I've came up with so far:
- The server runs PHP
- I am able to use GD, Imagick and FFMPEG
Regards
What I am trying to do is, placing a small static image on inside a video clip at server side, by making use of PHP.
For example, in TV news, you might see the culprit's photo at a corner when the anchor describes about a crime incident.
I've tried doing some research. And found the ffmpeg-php extension for PHP. When going through the docs, I couldn't find any ready made methods or classes that would insert a picture at a particular position if we pass the x and y positions like in GD.
But there is a ffmpeg_frame object available and I think it could be used to fetch frames one by one as an image. And we would do the insertion of our required image inside this using the GD functions. After insertion of the image in all the frames, we would generate the video using these frames. That's my idea. But that extension's docs doesn't reveal anything about creating the video from frames. Only talks about creating animate GIFs !
Would it be a big load for PHP by using the above way of inserting image in each frame and then generating the video from it(if it is possible) ?
Or are there any better ways available to do this insertion process. I mean any other better extensions or video libraries available ?
The aim is to insert an image in a video, which would be at a particular position through out the video.
Thanks in advance.
I want to mimic the bing video preview functionality, with a thumbnail preview, then onMouseOver, load and play a video file. I plan to use VideoJS (html5 + flash) for the video playback. I need to use ffmpeg to produce these video files.
How can I create a mp4 preview with video only, which contains 1s of every other minute of the full clip, and shrink resolution to a fixed width (maintaining aspect ratio, preferably with zoom crop) using php + ffmpeg command line?
I'm assuming it can be done somehow along the lines of cutting 1s clips, then combine the smaller clips, and re-encode for a final rescaled output.
*Edit: Using ffmpeg is a design requirement. Pulling out 1s clips, should be fairly easy, but combining them seems to be somewhat complex with ffmpeg. I don't want cycling thumbnails, I want a video preview which contains a number of 1s clips. Eg. runtime in seconds: 100-101, and 200-201 combined in a heavily compressed clip. I am asking for a command line example of how to do this in an efficient manner.
One way to do this in Windows (or wine) would be to use an Avisynth script. This would allow you to do all your desired transformations in one step. It's been a little while since I've used Avisynth but a very simple script might look like:
DirectShowSource("C:\file\to\encode.avi", audio=false) # Or another source filter
SelectRangeEvery(1440, 24) # outputs 24 frames every 1440 frames
BilinearResize(320,240) # resize to your desired resolution
Crop(...) # crop to reach desired aspect ratio
This could be extended to support various framerates and aspect ratios instead of hardcoding everything. The resulting .avs file can then be used as an input file to ffmpeg, provided that it has been compiled with --enable-avisynth.
I've an approach and i think that will work...
Using ffmpeg you can get thumbnails of the video at specific intervals.
So i will take about 5 to 10 thumbnails in the interval of 2 seconds and store them on my server with a unique name that identifies the video.
So when I mouse over i call the function which will load these images sequentially(which makes the user feel the video is fast forwarding..)
But in this format, we can't play sound when we mouse over ..
I don't know whether this is good but i know this will work..
Update:
I think it will be better to create a video using ffmpeg using the extracted images and play them while we mouse over.. This will be quite faster than loading a sequence of images. ffmpeg can be used to create a new video from a list of images.
I managed to do this now, by pulling out 1s clips, and convert them to mpeg. Then combine these mpeg files, by appending them into a single file. Then convert til mpeg file to mp4