Playing with Video Files on Linux
I have been recently playing with editing videos for some of my personal work. For e.g., obtaining the frames from a video, merging the frames back into a video after making some changes to the frame(s) or combining audio and video. The
ffmpeg utility on Linux is a very handy tool to do all these stuff and I list only a few commands that I have used for my use here:
Obtaining frames from a video
ffmpeg -i <Your_Video> -qscale 0 scene%d.jpg
It goes without saying that
<Your_Video>should be replaced with your own video without the
Merging frames into a video
ffmpeg -framerate <frame_rate> -i scene%d.jpg -c:v libx264 -profile:v high -crf 20 -pix_fmt yuv420p <newvideo>
The frame rate should be the same as the original frame rate of the video. so that the audio and video are in sync. Also, the video obtained will contain no sound. I will typically extract the audio from the original video and merge the extracted audio with the new modified video using some tool like KDenLive on Linux.
Finding the frame rate for a video
To properly create a video from frames, the frame rate should be the same as the original video so that when we merge the audio and video later on, both are synchronized. So the command below can be used to find the frame rate of the video:
ffprobe -v 0 -of csv=p=0 -select_streams v:0 -show_entries stream=r_frame_rate <Your_Video>
The output looks like this, indicating 25 fps:
Combining Audio and Video
I use the Lecture Notes. It is wonderfull which comes with a Lecture Presentation and a Lecture Videos extension. The Lecture Video extension allows me to annotate my lecture videos while I am recording my lecture. This allows for a more immersive experience for the audience as compared to just going through the slides.
The Lecture Videos extension saves the audio and video separately on the mobile device. One can merge them before exporting, but the merging takes a long time and the file size is very big. Instead, one can export the audio and video separately and use the following command of
ffmpeg to merge them:
ffmpeg -r <frame_rate> -i video1.h264 -i video1.3gp -b 1.5M video1.mp4
I am not an expert in video editing. Based on my needs I search the internet for answers and all the commands listed above were obtained from various sources on the internet including, but not limited to, stackoverflow. However, I do not remember the actual websites and I have modified the commands to suit my needs.