An amateurish evening in troubleshooting MP4 videos
I took some video clips during Durgapuja. Some were taken using my Canon camera some were taken on Samsung phones running Android. These are short , less than 5 mins, clips. I usually stitch the video clips to create a complete event video after the event and publish it to my YouTube channel. I use two ways to do the stitching . The first is mentioned in my old post here. The method produces plain concatenated longer video out of the sources without any effect or title slides added - good for quick hack. For effects and additional editing I use Kdenlive on a GNU / Linux computer - I use Debian based distro and like Ubuntu Studio for these creative works. All these are free (as in freedom) software and one does not need to go to a cloud based service to use any one of these.
I could not stitch Durgapuja videos with either of the above techniques this time. It was clear that something was wrong in one or many of my source files which was resulting in concatenated videos with black screens and audio out of sync. This lead to deeper troubleshooting of mp4 files. This time instead of using a search engine, I used a LLM and conversed with it during the entire troubleshooting process. I'm impressed with how Llama guided me to resolution. The following story is a summary of my learning from the entire troubleshooting episode - partly LLM generated as well. All the steps are tested / executed by me on my Ubuntu Studio Laptop running an old AMD cpu (pre Ryzen) with 16 G RAM.
In this article, we'll explore how to use FFmpeg to troubleshoot and fix common issues with MP4 files.
Problem Statement 1: Fixing Corrupted MP4 Files
"I have an MP4 file that's not playing properly. The video is stuttering, and the audio is out of sync. How can I fix this?"
One possible solution is to use FFmpeg to re-encode the video and audio streams. This can help repair any corruption or damage to the file.
Bash
ffmpeg -i input.mp4 -c:v libx264 -crf 18 -c:a aac -b:a 128k output.mp4
This command re-encodes the video stream using the H.264 codec and the audio stream using the AAC codec.
Problem Statement 2: Repairing MP4 Files with Missing Key Frames
"I'm getting an error message that says 'Missing key frame while searching for timestamp.' How can I fix this?"
To fix this issue, you can use FFmpeg to re-mux the MP4 file, which involves re-packaging the video and audio streams without re-encoding them.
Bash
ffmpeg -i input.mp4 -c copy -flags +global_header -map_metadata 0 output.mp4
This command re-muxes the MP4 file, preserving the original video and audio streams. Please note the last option is a zero not 'O' .
Problem Statement 3: Transcoding MP4 Files to Fix Compatibility Issues
"I have an MP4 file that's not playing properly on certain devices. How can I transcode it to fix compatibility issues?"
To transcode the MP4 file, you can use FFmpeg to convert it to a different codec or format. For example, you can transcode the file to the H.265 codec, which is more efficient and widely supported.
Bash
ffmpeg -i input.mp4 -c:v libx265 -crf 18 -c:a aac -b:a 128k output.mkv
This command transcodes the MP4 file to the H.265 codec, preserving the original audio stream.
Batch Transcoding Multiple MP4 Files
If you need to transcode multiple MP4 files, you can use a bash script to automate the process. Here's an example script:
Bash
#!/bin/bash # Set the input and output directories INPUT_DIR=./input OUTPUT_DIR=./output # Create the output directory if it doesn't exist mkdir -p "$OUTPUT_DIR" # Loop through all MP4 files in the input directory for file in "$INPUT_DIR"/*.mp4; do # Get the filename without the extension filename=$(basename "$file" .mp4) # Transcode the MP4 file to H.265 ffmpeg -i "$file" -c:v libx265 -crf 18 -c:a aac -b:a 128k "${OUTPUT_DIR}/${filename}.mkv"
This script loops through all MP4 files in the input directory, transcodes them to the H.265 codec, and saves the output files in the output directory. The transcoded ones are error free and good for concatenation process mentioned at the beginning. I used 9 such corrected clips and created this video. Please leave your comment if something does not work for you.













