I am trying to upload a video that is 640 x 480. Using FFmpeg I am trying to resize to a square but I am having much difficulty.
if i use s=320x320 in my command or scale 320x320
->setVideoScale(320, 320)
the image is distored.
If i change the aspect ratio 1:1 i get black bars.
is there a way to actually just cut a RECT in the video. IE i want to be able to re save the video and it only capture
x 0 to 320
y 0 to 320
or whatever bounds I pick.
Basically I do not want to re-size I just want to be able to re record a certain portion of the video. is this possible?
Check out FFMPEG's video filters. There is a crop option that will do what you're asking for, specifically the first example.
Depending on the library you're using to interface with FFMPEG, something like the following might work per your example:
->addCommand('-vf', 'crop=320:320:0:0');
Related
I'm trying to dynamically change the resolution of videos uploaded to a server via PHP, using FFmpeg. IE, I want to preserve portrait or landscape orientation - if Y is higher than X, I want to change Y to 320 and X to a corresponding value, and vice versa. I'm not having trouble with the resizing itself - it's quite straightforward, actually. What I'm having trouble with is detecting which dimension is larger.
I grabbed this solution off StackOverflow: how to check if video is landscape or portrait using ffmpeg php?
However, it doesn't appear to be working. While I track down what isn't working - I'm assuming the way the output is formatted has changed since that solution was posted, and it now needs to be parsed differently - I wanted to ask if there was a better way to do this. I'm open to using FFmpeg or a PHP-based solution.
ffmpeg -i input.jpg -vf 'scale=320:320:force_original_aspect_ratio=decrease' output.png
According to FFmpeg documentation, the force_original_aspect_ratio option is useful to keep the original aspect ratio when scaling:
force_original_aspect_ratio
Enable decreasing or increasing output video width or height if
necessary to keep the original aspect ratio. Possible values:
disable
Scale the video as specified and disable this feature.
decrease
The output video dimensions will automatically be decreased if
needed.
increase
The output video dimensions will automatically be increased if
needed.
Use decrease to make the largest dimension to 320, or use increase to make the smallest dimension to 320.
Have a look at the resizing page on the FFmpeg wiki. You basically want to look specifically at this section:
Sometimes there is a need to scale the input image in such way it fits
into a specified rectangle, i.e. if you have a placeholder (empty
rectangle) in which you want to scale any given image. This is a
little bit tricky, since you need to check the original aspect ratio,
in order to decide which component to specify and to set the other
component to -1 (to keep the aspect ratio). For example, if we would
like to scale our input image into a rectangle with dimensions of
320x240, we could use something like this:
ffmpeg -i input.jpg -vf scale="'if(gt(a,4/3),320,-1)':'if(gt(a,4/3),-1,240)'" output_320x240_boxed.png
You can obviously use this for videos as well as for images.
You can use a if statement to select the larger one
-vf scale='if(gt(iw,ih),320:-2)':'if(gt(iw,ih),-2:320)'
FYI, the correct way to do this is to use FFprobe, which comes with FFmpeg.
https://www.ffmpeg.org/ffprobe.html
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 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
I use tumblr to share videos and, unfortunately, when re-posting a video to your tumblelog it uses the embed code at 400x336px. This is neither widescreen nor very large.
So I'm trying to set the player to better size and I'm finding that the youtube player really comes in many sizes. For example, when copying the embed code for and HD video I get the sizes 560x340, 640x385, and 853x505. Then when I look at the embed code for a SD video I get the sizes 425x344, 480x385, and 640x505.
I was having the best luck with setting the player to 640 x 505 as it was large enough for HD and perfect for SD.
print str_replace( array('400', '336'), array('640', '505'), $video_player_html);
Has anyone dealt with trying to find a good standard embed size for a site? One thing that would make this a lot easier is to know whether the video was HD or not - however, that info is not provided in the embed code.
I guess I am actually more interested in knowing the right ratio (or specific size) to use that works the best for youtube's mixed content.
Update
Since HD seems to be .5625 ratio (16/9) I'm thinking that I can just decide on a hight and multiply it by the ratio to get the proper width each time (plus 23px for youtube bar).
You don't have to worry about the sizes youtube offers. When you copy the embed code, paste it into a text editor and you'll see it contains width and height parameters in two places. You can change those values to anything at all and the player will resize. So grab your JRuler, figure out what size would be ideal for your tumblelog, and just edit the player code each time.
It's kind of a pain, but it gets you the result you want.
I need to be able to determine if a part of an image (outside of predetermined crop marks) contains any image content.
Is there a way with ImageMagick (specifically the php interface) to do this?
Scenario:
The canvas is 8.5x11 with .5 in margins on the top, left, and bottom edges and a 1 in margin on the right edge. The image needs to fit within the crop marks for printing.
Normally I use Photoshop actions to do this, but I am trying to automate the process.
Replace everything within crop marks with black/white rectangle, do a histogram of the resulting image and analyze it?
I can do this with command-line version of ImageMagick, but dont know how to express it with PHP api.