Bob Eisenman Posted December 1, 2017 Share Posted December 1, 2017 Using ffmpeg to trim videos, recombine segments of videos and to merge in an audio track. I decided to learn to use ffmpeg awhile ago to do simple video tasks like trimming and resizing. I learned most of what I know by asking Google questions like: how do I resize a video using ffmpeg? ffmpeg works great when it works but there are times when I know my objective is not attainable with the knowledge that Google has provided. So..know when you are winning the battle and when you are not. Also use a recent version of ffmpeg from the ffmpeg website downloads page. ffmpeg works with codecs that may not be compatible with the codec used in your source mp4 video. In general it is ok to input a source video directly from your camera but the output video (codec)from ffmpeg processing is likely to have been changed. When combining videos with ffmpeg you should only use the videos whose codecs have been converted to ffmpeg's codec. Also, only combine videos of the same size (ex. 1280x720). Suppose I want to trim each of two of my camera videos to a particular segment, combine the segments and then merge in an audio track. The process would be: 1-trim video#1 2-trim video#2 3-combine video#1with video#2 4-strip out the audio part of the combined videos 5-merge in an audio track with the audio-less video track To trim a video with ffmpeg I use the command (in a terminal window located in the same directory as the video files and the ffmpeg executable) ffmpeg -i input.mp4 -ss 00:00:30 -t 00:00:45 output-trim.mp4 (-i input.mp4) is the name of the video from the camera (-ss 00:00:30) is the position in hrs:mins:secs where the trim starts (ex @30 seconds) (-t 00:00:45) is the duration of the video to be included after the trim timepoint (ex for 45 seconds) (output-trim.mp4) is the name of the trimmed video segment To combine two video segments ffmpeg can use a text file with the names of the video files to be combined. For example, a simple text file document called 'playlist.txt' containing the following two entries: file 'output-trim1.mp4' file 'output-trim2.mp4 To combine the the two playlist videos the playlist, trimmed videos and terminal window must be in the same folder directory (the bin directory of the installed ffmpeg program). The videos are combined into the mp4 video called output-playlist.mp4 by running the following command: ffmpeg -f concat -i playlist.txt -c copy output-playlist.mp4 The output video has both a video and an audio component. To merge in an audio track of the same length I must first remove the native audio track of the video 'output-playlist.mp4' by running the following command line in a terminal window: ffmpeg -i output-playlist.mp4 -vcodec h264 -an video.h264 where (-vcodec h264) sets the ouput video codec to h264 where (-an) tells the software not to include the audio track where (video.h264) is the video only file to be used in the merge To merge a selected mp3 audio file with the video.h264 visual-only video I use the command: ffmpeg -i video.h264 -i audio.mp3 merged.mp4 where (-i video.h264) is the video stripped of the audio track where (-i audio.mp3) is a trimmed mp3 file named audio.mp3. I use Audacity to trim the audio, add fade outs and export to an mp3 file. WAV file format works as well. where (merged.mp4) is the name of the new video composesd of the video segmants and the mp3 audio track command line sequence: ffmpeg -i input.mp4 -ss 00:00:00 -t 00:00:00 output-trim.mp4 note: you need one line of this command per video segment created ffmpeg -f concat -i playlist.txt -c copy output-playlist.mp4 note: don't forget to create the playslist file and to use segments with the same video dimensions (ex. 1280x720) ffmpeg -i output-playlist.mp4 -vcodec h264 -an video.h264 ffmpeg -i video.h264 -i audio.mp3 merged.mp4 note: I use Audacity to create a trimmed audio file in mp3 format Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.