First, let’s say you have a large video file that needs to be compressed and converted into a format suitable for online playback. You can use the following command:
# This script uses the ffmpeg command to compress and convert a video file into a suitable format for online playback.
# The -i flag specifies the input file, in this case "input_video.mp4".
# The -c:v flag specifies the video codec to use, in this case "libx265" which is a high-efficiency video coding format.
# The -preset flag specifies the encoding speed, in this case "veryfast" which will result in a faster conversion but slightly larger file size.
# The last argument is the output file name, in this case "output_video.webm".
ffmpeg -i input_video.mp4 -c:v libx265 -preset veryfast output_video.webm
In this example, we’re using FFmpeg to convert an MP4 video file (input_video.mp4) into a WebM format (output_video.webm). We’ve also specified the libx265 codec and veryfast preset for faster encoding times.
The output of this command will be a smaller, compressed version of your original video file that is optimized for web streaming. This can significantly reduce loading times and improve overall performance on websites or online platforms where videos are being streamed.
Now let’s say you want to add custom watermarks or overlays to the video during transcoding using FFmpeg. You can use the following command:
#!/bin/bash
# This script uses FFmpeg to transcode a video and add a custom watermark or overlay to it.
# The input video is specified as "input_video.mp4" and the output video will be named "output_video.webm".
# The "-i" flag indicates the input file.
# The "-c:v" flag specifies the video codec to be used, in this case "libx265".
# The "-preset" flag sets the encoding speed, with "veryfast" being the fastest option.
# The logo or overlay image is specified as "logo.png" and will be added to the video during transcoding.
# The "-i" flag indicates the input file.
# The "-filter_complex" flag allows for complex filtergraphs to be created.
# In this case, the "overlay" filter is used to add the logo to the video.
# The "main_w" and "overlay_w" variables represent the width of the main video and the overlay image, respectively.
# The "[out]" label is used to store the output of the filter for later use.
# The "[in][out]" label is used to specify the input and output for the next filter.
# In this case, the "overlay" filter is used again to add the previously created overlay to the video.
# The "0:0" coordinates indicate that the overlay should be placed at the top left corner of the video.
# Finally, the output video is named "output_video.webm" and the transcoding process begins.
ffmpeg -i input_video.mp4 -c:v libx265 -preset veryfast -i logo.png -filter_complex "overlay=main_w-overlay_w[out]; [in][out] overlay=0:0" output_video.webm
In this example, we’re using FFmpeg to convert an MP4 video file (input_video.mp4) into a WebM format (output_video.webm). We’ve also specified the libx265 codec and veryfast preset for faster encoding times.
We’re adding a custom watermark or overlay to the video using the logo.png image file, which is added as an input source (-i) with its own filter chain (overlay=main_w-overlay_w[out]). The resulting output from this filter chain is then merged back into the original video stream using another overlay filter (overlay=0:0).
The output of this command will be a smaller, compressed version of your original video file with custom watermarks or overlays added during transcoding. This can significantly improve branding and marketing efforts for online videos.
In terms of performance optimization, FFmpeg provides various options to optimize the encoding process based on specific requirements such as CPU usage, memory consumption, and output format. For example, using hardware acceleration with CUDA or NVENC can significantly reduce processing time and improve overall video quality. Additionally, adjusting the preset value for libx265 can also affect the trade-off between encoding speed and video quality.
Overall, FFmpeg is a powerful tool for optimizing video files for web streaming with various customization options to meet specific requirements.