Ffmpeg. Tricks and tricks

Introduction

FFmpeg is a multimedia processor, a set of libraries that allow you to record, process and convert video and / or audio materials into various formats. With this tool, you can make video editing of almost any complexity. It is used by many players, converters, editors and computer vision libraries. In this article, I'll share some of the FFmpeg tricks and tricks that I often use in my work.





Trim video by time

Quite often, you need to cut out a small fragment from the video from n to m seconds. To do this, you need to specify the second with which to cut and the duration.





ffmpeg -i i.mp4 -ss 00:01:00 -t 00:02:00 -c copy o.mp4
      
      



The parameter -ss



indicates the starting point and -t



the duration.





Important note! If the duration of the video is 5 minutes, we will specify the starting point at 00:04:00, and the duration at 00:02:00, the duration of the final video will be 1 minute.





Here and further in the text of the article, i.mp4



is the path to the input file, and o.mp4



- to the output.





Create video from photos

Suppose we have a security camera that took a photo every minute during the day, and we want to get one continuous video.





, .jpg , 10 FPS, H.264 .





ffmpeg -framerate 10 -pattern_type glob -i '*.jpg' -c:v libx264 o.mp4
      
      



— .





ffmpeg -i o.mp4 -r 1 -q:v 2 -f image2 img-3%d.jpeg
      
      



, list.txt , , :





file 'video1.mp4'
file 'video2.mp4'
file 'videoN.mp4'
      
      



:





ffmpeg -f concat -i list.txt -c copy o.mp4
      
      



, , , :





for f in ./*.mp4; do echo "file '$f'" >> list.txt; done
      
      



:





printf "file '%s'\n" ./*.mp4> list.txt
      
      



:





ffmpeg -i i.mp4 -c:v copy -an o.mp4
      
      



:





ffmpeg -i i.mp4 -vn 0.wav
      
      



, , 16:9 3:4, .





ffmpeg -i i.mp4 -filter:v "crop=w:h:x:y" o.mp4
      
      



crop , , .





(stack video)

, , , :





  • .





  • .





:





ffmpeg -i i0.mp4 -i i1.mp4 -filter_complex hstack=inputs=2 o.mp4
      
      



:





ffmpeg -i i0.mp4 -i i1.mp4 -filter_complex vstack=inputs=2 o.mp4
      
      



2x2:





ffmpeg \
-i i0.mp4 -i i1.mp4 -i i2.mp4 -i i3.mp4 \
-filter_complex \
"[0:v][1:v]hstack=inputs=2[top]; \
[2:v][3:v]hstack=inputs=2[bottom]; \
[top][bottom]vstack=inputs=2[v]" \
-map "[v]" \
o.mp4
      
      



3x2:





ffmpeg \
-i i0.mp4 -i i1.mp4 \
-i i2.mp4 -i i3.mp4 \
-i i4.mp4 -i i5.mp4 \
-filter_complex \
"[0:v][1:v][2:v]hstack=inputs=3[top];\
[3:v][4:v][5:v]hstack=inputs=3[bottom];\
[top][bottom]vstack=inputs=2[v]" \
-map "[v]" \
o.mp4
      
      



ffmpeg -i i.mp4 -vf "transpose=0" o.mp4
      
      



transpose :





  • 0 - 90 . .





  • 1 - 90 .





  • 2 - Rotate 90 degrees counterclockwise.





  • 3 - Rotate 90 degrees clockwise and rotate vertically.





Afterword

FFmpeg is a very powerful video processing tool. With its help, having a cheat sheet on its capabilities, you can save a lot of time - it is much faster to enter a few commands to process a video than to launch an editor.





If you know more useful tricks and tricks - you are welcome to share them in the comments. Thank you!








All Articles