WASMAI:FFmpeg
Using FFMPEG with WASMAI
FFMPEG is a powerful, open-source multimedia framework that can be used for video and audio processing. It supports a wide range of formats and is widely used for tasks such as video encoding, decoding, and editing. Its versatility and ease of use make it a go-to tool for developers and creators alike who need to manipulate video or audio files.
Combining PNG Sequences with FFMPEG
WASMAI generates high-quality PNG image sequences for animations or graphics. Once you have these images, FFMPEG can be used to easily stitch them together into a video. This is a great way to create smooth animations or dynamic visuals for your web-based projects. Simply use FFMPEG’s command-line tools to process and merge the images into a single video file.
One important thing to note when using FFMPEG with PNG sequences is that the height of the exported images must be divisible by two. This is a requirement from FFMPEG to ensure proper processing of the video frames. So, before you start, make sure your image dimensions meet this criterion to avoid any issues during the video encoding process.
Downloading FFMPEG
FFMPEG is completely free to use and is available on all major operating systems, including Windows, Linux, and macOS. You can easily download the latest version of FFMPEG from the official website at https://ffmpeg.org/download.html. It’s a cross-platform tool, making it a great choice for anyone working across different environments or platforms.
Example Usage of FFMPEG with WASMAI
Here’s an example of how you can use FFMPEG to combine a sequence of images generated by WASMAI into a video:
ffmpeg -framerate 30 -i wasmai_%04d.png -c:v libx264 -crf 23 -preset slow -pix_fmt yuv420p wasmai.mp4
Let’s break down what each of these parameters does:
- -framerate 30: This specifies the frame rate of the resulting video. In this case, the video will play at 30 frames per second (FPS), which is a standard frame rate for smooth video playback.
- -i wasmai_%04d.png: This is the input pattern for the image sequence. The "%04d" is a placeholder for sequential numbers, ensuring FFMPEG looks for images like "wasmai_0001.png", "wasmai_0002.png", etc. The number of digits (in this case, 4) is specified by "%04d", allowing for a properly ordered image sequence.
- -c:v libx264: This tells FFMPEG to use the H.264 codec for video encoding. H.264 is a widely-used codec for video compression, providing a good balance of quality and file size.
- -crf 23: The CRF (Constant Rate Factor) parameter controls the video quality. A lower number means higher quality, and a higher number means lower quality. The default value is 23, which is a good balance for most uses.
- -preset slow: This controls the encoding speed versus compression ratio. The "slow" preset will give you better compression (smaller file size and better quality), but the encoding process will take longer. You can use "fast" or "medium" for quicker encoding, but with larger file sizes or lower quality.
- -pix_fmt yuv420p: This sets the pixel format to YUV 4:2:0, which is a standard format for video playback. It ensures compatibility with most devices and media players.
- wasmai.mp4: This is the output file name and format. In this case, the video will be saved as "wasmai.mp4".
This command will take your PNG images generated by WASMAI and combine them into a video at 30 frames per second, using the H.264 codec with a good balance of compression and quality.
More Useful and Interesting Examples of Using FFmpeg.
Below are some useful FFmpeg commands to apply various effects to your videos. Click on any of the following to jump directly to the corresponding example:
BW. Stunning Black and White.
Here is an example of using FFmpeg to generate a black and white video from a series of images, making it a simple yet powerful way to create monochrome mp4 outputs.
ffmpeg -framerate 30 -i wasmai_%04d.png -c:v libx264 -crf 23 -preset slow -pix_fmt yuv420p -vf "hue=s=0" wasmai.mp4
NEGATIVE. Striking Negative Effect.
Here is an example of using FFmpeg to create a negative effect (color inversion) on a video:
ffmpeg -framerate 30 -i wasmai_%04d.png -c:v libx264 -crf 23 -preset slow -pix_fmt yuv420p -vf "negate" wasmai_negative.mp4
SEPHIA. Timeless Vintage Look with the Sepia Effect.
Here is an example of using FFmpeg to apply a built-in sepia effect to a video, giving it a warm, vintage look by adjusting the color channels.
ffmpeg -framerate 30 -i wasmai_%04d.png -c:v libx264 -crf 23 -preset slow -pix_fmt yuv420p -vf "colorchannelmixer=.393:.769:.189: .349:.686:.168: .272:.534:.131" wasmai_sepia.mp4
CONTRAST. Visual Impact with Enhanced Contrast.
Here is an example of using FFmpeg to increase the contrast of a video. This command enhances the contrast of the video, making the dark areas darker and the light areas lighter for a more dynamic visual effect.
ffmpeg -framerate 30 -i wasmai_%04d.png -c:v libx264 -crf 23 -preset slow -pix_fmt yuv420p -vf "eq=contrast=2.0" wasmai_contrast.mp4
NOISE. Add Dynamic Noise.
Here is an example of using FFmpeg to add noise to a video. This command adds noise to the video. The alls=80
parameter controls the intensity of the noise-higher values will increase the noise effect, while lower values will reduce it.
ffmpeg -framerate 30 -i wasmai_%04d.png -c:v libx264 -crf 23 -preset slow -pix_fmt yuv420p -vf "noise=alls=80:allf=t+u" wasmai_noise.mp4
BLUR. Dreamy Look with the Blur Effect.
Here is an example of using FFmpeg to apply a blur effect to a video. This command applies a blur effect to the video. The boxblur=10:1
parameter controls the intensity of the blur-higher values will create a stronger blur effect.
ffmpeg -framerate 30 -i wasmai_%04d.png -c:v libx264 -crf 23 -preset slow -pix_fmt yuv420p -vf "boxblur=10:1" wasmai_blur.mp4
COMBINED. Create Custom Video Effects by Combining Multiple Filters.
Here is an example of using FFmpeg to combine several effects into a single video. This command applies a sepia effect, followed by contrast enhancement and noise addition. This command processes the video in stages: it first applies the sepia filter, then increases the contrast, and finally adds noise. You can adjust the parameters of each effect to achieve the desired outcome, allowing you to create more complex and customized video effects.
ffmpeg -framerate 30 -i wasmai_%04d.png -c:v libx264 -crf 23 -preset slow -pix_fmt yuv420p -vf "colorchannelmixer=.393:.769:.189: .349:.686:.168: .272:.534:.131, eq=contrast=2.0, noise=alls=50" wasmai_combined.mp4
VIBRANT. Colorful Effect Using FFmpeg.
Here is an example of using FFmpeg to increase the saturation of a video, creating a more vibrant effect. This command increases the saturation of the video, making the colors more vivid and intense. The saturation=2.0
parameter can be adjusted to increase or decrease the vibrancy of the colors in the video.
ffmpeg -framerate 30 -i wasmai_%04d.png -c:v libx264 -crf 23 -preset slow -pix_fmt yuv420p -vf "eq=saturation=2.0" wasmai_vibrant.mp4
FLIP.Applying a Mirror Effect: Flip Your Video Horizontally
Here is an example of using FFmpeg to apply a mirror effect to a video. This effect is achieved by flipping the video horizontally, essentially creating a mirror image. The command uses the hflip
filter, which flips the entire video around its vertical axis. This creates a reversed version of the video, ideal for artistic effects or specific visual styles.
Additionally, there are other types of flips you can apply to your video:
- hflip: Horizontal flip, as demonstrated in the example. This flips the video along the vertical axis.
- vflip: Vertical flip, which mirrors the video along the horizontal axis, flipping the top and bottom.
These flip effects can be combined with other filters to create more complex visual transformations.
ffmpeg -framerate 30 -i wasmai_%04d.png -c:v libx264 -crf 23 -preset slow -pix_fmt yuv420p -vf "hflip" wasmai_mirror.mp4
Fade In/Out. Smooth Transitions at the Beginning and End of Your Video
Here is an example of using FFmpeg to apply a fade in effect at the beginning of the video and a fade out effect at the end. These transitions provide a smooth visual effect, making the start and end of your video more cinematic. The fade
filter is used, where:
- fade=t=in: This parameter specifies the fade-in effect, where the video gradually becomes visible.
- st=0: The start time for the fade effect. In this case, it begins at the start of the video (0 seconds).
- d=2: The duration of the fade effect in seconds. In this case, the fade-in lasts for 2 seconds.
- fade=t=out: This parameter specifies the fade-out effect, where the video gradually becomes transparent.
- st=3: The start time for the fade-out effect. Here, it starts at the 3-second mark in the video.
- d=2: The duration of the fade-out effect in seconds. It lasts for 2 seconds, gradually fading the video to black.
This combination of fade-in and fade-out effects creates a smooth introduction and conclusion for your video, perfect for creating a professional look.
ffmpeg -framerate 30 -i wasmai_%04d.png -c:v libx264 -crf 23 -preset slow -pix_fmt yuv420p -vf "fade=t=in:st=0:d=2,fade=t=out:st=3:d=2" wasmai_fade.mp4
Edge Detection. Highlighting the Contours of Your Video
Here is an example of using FFmpeg to apply an edge detection effect to a video. The edgedetect
filter is used to highlight the edges in the video, which helps to emphasize the outlines of objects and create a high-contrast, artistic look. This effect is often used in image processing and computer vision applications, but it can also create interesting visual effects in videos. This effect is useful for creating artistic, sketch-like visuals or emphasizing the contours of objects within a scene. You can use it to enhance the structural details in your video, making it more abstract and visually striking.
- edgedetect: The edge detection filter highlights the edges in the video by using an algorithm that detects the differences between neighboring pixels, creating a high-contrast outline of objects.
ffmpeg -framerate 30 -i wasmai_%04d.png -c:v libx264 -crf 23 -preset slow -pix_fmt yuv420p -vf "edgedetect" wasmai_edges.mp4
FILTERS. Complete Guide to FFmpeg Video Filter Parameters (-vf)
FFmpeg offers a wide variety of video filters, controlled using the -vf
parameter. These filters allow you to manipulate video files in numerous ways, from basic transformations like resizing to complex visual effects. Below is a list of commonly used -vf
filters and their descriptions:
- hue: Adjusts the hue, saturation, and lightness of the video. You can manipulate the color balance using this filter. Example:
hue=s=0
(desaturates the video to black and white). - eq: Adjusts the brightness, contrast, and saturation of the video. Example:
eq=contrast=2.0:saturation=1.5
to increase contrast and saturation. - fade: Adds fade-in or fade-out effects to the video. Example:
fade=t=in:st=0:d=2
for a 2-second fade-in at the start. - boxblur: Applies a blur effect to the video. Example:
boxblur=10:1
to create a moderate blur effect. - colorchannelmixer: Adjusts the color balance of the video by mixing individual color channels. Example:
colorchannelmixer=.393:.769:.189: .349:.686:.168: .272:.534:.131
for a sepia effect. - negate: Applies a color inversion (negative) effect, making dark areas light and light areas dark.
- noise: Adds noise to the video. Example:
noise=alls=80:allf=t+u
adds noise with an intensity of 80. - edgedetect: Applies edge detection to the video, highlighting the edges of objects. This is typically used to create artistic or abstract visuals.
- crop: Crops the video to a specified area. Example:
crop=640:480:0:0
crops the top-left 640x480 portion of the video. - scale: Resizes the video to a specified resolution. Example:
scale=1280:720
scales the video to 1280x720 resolution. - transpose: Rotates the video. Example:
transpose=1
rotates the video by 90 degrees clockwise. - flip: Flips the video horizontally or vertically. Example:
flip=h
to flip horizontally (left to right). - vflip: Vertically flips the video (top to bottom).
- hflip: Horizontally flips the video (left to right).
- tinterlace: Applies interlacing to the video, which is useful for old TV formats. Example:
tinterlace
applies the effect. - rotate: Rotates the video by a specified angle. Example:
rotate=PI/2
rotates the video by 90 degrees clockwise (PI/2 radians). - lenscorrection: Corrects lens distortion in the video. Useful for fisheye and wide-angle lens corrections.
- sepia: Applies a sepia effect to the video, giving it a warm, vintage look.
- perspective: Applies perspective transformation to the video. Example:
perspective=x0=0:y0=0:x1=1000:y1=0:x2=1000:y2=1000:x3=0:y3=1000
for a custom perspective distortion. - transpose: Rotates the video. Example:
transpose=2
rotates the video 90 degrees counterclockwise. - sharpen: Sharpens the video. Example:
sharpen=3.0
applies a high amount of sharpening. - setpts: Modifies the presentation timestamps of the video. This is commonly used for time remapping, such as creating slow-motion or fast-motion effects.
These filters can be chained together to create more complex effects, allowing you to customize and manipulate videos in numerous ways to fit your creative vision. By combining multiple -vf
parameters, you can achieve nearly any video effect you desire. Below are practical examples using various FFmpeg video filters:
-
Hue Adjustment: Adjusts the hue and saturation of the video.
ffmpeg -framerate 30 -i wasmai_%04d.png -c:v libx264 -crf 23 -preset slow -pix_fmt yuv420p -vf "hue=s=0" wasmai_hue.mp4
-
Contrast and Saturation Adjustment: Increases the contrast and saturation of the video.
ffmpeg -framerate 30 -i wasmai_%04d.png -c:v libx264 -crf 23 -preset slow -pix_fmt yuv420p -vf "eq=contrast=2.0:saturation=1.5" wasmai_eq.mp4
-
Scaling: Resizes the video to 1280x720 resolution.
ffmpeg -framerate 30 -i wasmai_%04d.png -c:v libx264 -crf 23 -preset slow -pix_fmt yuv420p -vf "scale=1280:720" wasmai_scale.mp4
-
Rotation: Rotates the video 90 degrees clockwise.
ffmpeg -framerate 30 -i wasmai_%04d.png -c:v libx264 -crf 23 -preset slow -pix_fmt yuv420p -vf "rotate=PI/2" wasmai_rotate.mp4
These examples demonstrate how to apply basic video transformations and effects using FFmpeg. By combining these filters, you can create custom visual effects tailored to your video editing needs.
More Advanced Techniques of FFmpeg.
Later...:)