DEV Community

bill lee
bill lee

Posted on

How to Convert MOV to MP4 by Re-encoding Only the Audio

Your upload pipeline expects an MP4 with H.264 video and AAC audio. The MOV you received already has H.264 video, but its audio is PCM. You can copy the video into the MP4 and encode just the audio. In this test, all 72 compressed video packets matched the source.

AI disclosure — This article was generated with AI. The commands and results come from a local FFmpeg 8.1.2 test.

Make two MOV files with the same video

Run the commands in a fresh directory with ffmpeg and ffprobe installed. The first command needs an FFmpeg build with libx264. It creates a three-second test pattern with AAC audio. The second takes the same video stream and gives it a PCM audio track.

ffmpeg -f lavfi -i 'testsrc2=size=320x180:rate=24:duration=3' \
  -f lavfi -i 'sine=frequency=440:sample_rate=48000:duration=3' \
  -c:v libx264 -pix_fmt yuv420p -c:a aac -b:a 128k demo-aac.mov

ffmpeg -i demo-aac.mov \
  -f lavfi -i 'sine=frequency=440:sample_rate=48000:duration=3' \
  -map 0:v:0 -map 1:a:0 -c:v copy -c:a pcm_s16le demo-pcm.mov
Enter fullscreen mode Exit fullscreen mode

Inspect the streams before choosing a command. Both inputs end in .mov, but the audio codecs differ.

codecs() {
  ffprobe -v error -show_entries stream=codec_type,codec_name -of csv=p=0 "$1"
}

codecs demo-aac.mov
codecs demo-pcm.mov
Enter fullscreen mode Exit fullscreen mode
Input Video codec Audio codec
demo-aac.mov h264 aac
demo-pcm.mov h264 pcm_s16le

Change only what your destination needs

If both tracks already meet the destination requirements, copy both. If the video qualifies and the destination requires AAC, copy the video and encode the audio.

ffmpeg -i demo-aac.mov \
  -map 0:v:0 -map 0:a:0 -c copy -movflags +faststart copied.mp4

ffmpeg -i demo-pcm.mov \
  -map 0:v:0 -map 0:a:0 -c:v copy -c:a aac -b:a 128k \
  -movflags +faststart audio-converted.mp4

codecs copied.mp4
codecs audio-converted.mp4
Enter fullscreen mode Exit fullscreen mode

Both outputs report H.264 and AAC. The -map options select the first video track and the first audio track. If your file has alternate audio or subtitles, decide which tracks the output needs before adapting the command. +faststart moves the MP4 index to the front of the file. FFmpeg stream handling · MP4 muxer options

Diagram based on the local PCM-to-AAC test.

Verify the video packets

Seeing h264 on both sides tells you the codec. An H.264-to-H.264 re-encode would show the same name. To check what survived the conversion, hash the compressed payload of each video packet and compare the lists in order. FFprobe packet and data hash options

video_packets() {
  ffprobe -v error -select_streams v:0 -show_packets \
    -show_entries packet=data_hash -show_data_hash sha256 \
    -of csv=p=0 "$1"
}

video_packets demo-pcm.mov > before.sha256
video_packets audio-converted.mp4 > after.sha256
wc -l before.sha256 after.sha256
diff -u before.sha256 after.sha256
Enter fullscreen mode Exit fullscreen mode

Each list had 72 hashes, and diff exited successfully with no output. That checks video packet payloads in this sample. The container, timestamps and audio are outside that comparison. For a production upload, also open the output in the destination player and check playback and sync.

Top comments (1)

Collapse
 
fredabila profile image
Fred Abila •

Copying the H.264 video and encoding only PCM to AAC is the right split — a full transcode would be the thing that quietly shifts timestamps or adds another generation of artefacts. One extra preflight: confirm the video is actually h264 (not proxy ProRes or a phone HEVC .mov with a .mov extension) before -c:v copy, and add -movflags +faststart as you did so the MP4 can start playing before the upload finishes. If the source has a second audio track or a data/timecode stream, an explicit -map 0:v:0 -map 0:a:0 keeps those from unexpected muxer behaviour.