Extracting the Last Seconds of a GoPro Video Losslessly with FFmpeg While Keeping Telemetry Info and Original Stream Order
GoPro cameras are popular for capturing high-quality video footage during various activities. They typically record video in four streams: video, audio, telemetry, and text overlay. This article covers the process of extracting the last few seconds of a GoPro video using FFmpeg, while keeping the telemetry information and maintaining the original stream order.
Understanding GoPro Video Streams
GoPro video files contain four streams of data:
- Video stream: Contains the actual video footage
- Audio stream: Contains the audio track
- Telemetry stream: Contains data such as GPS coordinates, speed, and acceleration
- Text overlay stream: Contains information displayed on the screen, such as the current mode, resolution, and battery level
These streams are typically multiplexed (muxed) together into a single file, but they can be demultiplexed (demuxed) and processed separately.
Using FFmpeg to Extract the Last Seconds of a GoPro Video
FFmpeg is a powerful, open-source tool for handling multimedia files. It can be used to extract the last few seconds of a GoPro video while preserving the original stream order and telemetry information.
Step 1: Demultiplex the GoPro Video
The first step is to demultiplex the GoPro video file using FFmpeg. This will separate the video, audio, telemetry, and text overlay streams into individual files.
ffmpeg -i input.mp4 -vcodec copy -an -map 0:v:0 output\_video.mp4 -map\_metadata 0 -fflags +genpts output\_telemetry.txt
This command does the following:
- Demultiplexes the input.mp4 file
- Copies the video stream to a new file called output\_video.mp4
- Excludes the audio stream
- Maps the telemetry metadata to the output\_telemetry.txt file
- Sets the presentation time stamps (PTS) for the output files
Step 2: Extract the Last Seconds of the Video
The next step is to extract the last few seconds of the video using FFmpeg's segment muxer.
ffmpeg -ss -1 -t 2 -i output\_video.mp4 -c copy last\_two\_seconds.mp4
This command does the following:
- Specifies the start time (SS) as 1 second before the end of the video
- Sets the duration (T) to 2 seconds
- Inputs the output\_video.mp4 file from the previous step
- Copies the video stream to the last\_two\_seconds.mp4 file
Step 3: Remux the Last Seconds of the Video
The final step is to remux the last few seconds of the video with the audio, telemetry, and text overlay streams using FFmpeg.
ffmpeg -i last\_two\_seconds.mp4 -i output\_audio.mp3 -i output\_telemetry.txt -i output\_text.srt -filter\_complex "[0:v][1:a][2:0][3:0]concat=n=4:v=1:a=1:c=0" -c:v libx264 -crf 18 -preset slow -c:a aac -b:a 192k -map 0:v:0 -map 0:a:0 -fflags +genpts output.mp4
This command does the following:
- Inputs the last\_two_seconds.mp4, output\_audio.mp3, output\_telemetry.txt, and output\_text.srt files
- Concatenates the video, audio, telemetry, and text overlay streams into a single file
- Encodes the video using the H.264 codec with a constant rate factor (CRF) of 18 and the slow preset
- Encodes the audio using the AAC codec with a bitrate of 192k
- Maps the first video stream and first audio stream to the output file
- Sets the presentation time stamps for the output file
This article covered the process of extracting the last few seconds of a GoPro video using FFmpeg, while preserving the telemetry information and maintaining the original stream order. The steps involved demultiplexing the GoPro video, extracting the last few seconds of the video, and remultiplexing the video with the audio, telemetry, and text overlay streams.
References
- FFmpeg: A powerful, open-source tool for handling multimedia files.
- FFmpeg MP4 Muxer Documentation: Information on the MP4 muxer used in this article.