Losslessly repair mp4 files without interleaving

I added exiftool commands to move the remaining metadata and remove the encoder tag from the remuxed files. Below I have included the script I used to go thru my library. I am batching by TV Show I encounter to have errors, but it could easily be modified if anybody else has the same problems.

D:
CD "D:\TV Library\Star Trek - The Original Series (1966)"
FOR /F "tokens=*" %%G IN ('dir /b /s *.m4v') DO ffmpeg.exe -i "%%G" -c copy -map 0 -map -0:3 -map -0:5 -movflags +faststart -map_metadata 0 "E:\Workfile\MediaFixed\%%~nG.m4v"
FOR /F "tokens=*" %%G IN ('dir /b /s *.m4v') DO exiftool -tagsfromfile "%%G" "E:\Workfile\MediaFixed\%%~nG.m4v"
FOR /F "tokens=*" %%G IN ('dir /b /s *.m4v') DO exiftool -encoder="" "%%G" "E:\Workfile\MediaFixed\%%~nG.m4v"
E:
CD "E:\Workfile\MediaFixed"
DEL /S "E:\Workfile\MediaFixed\*.m4v_original"
1 Like

Software Used
FFmpeg and FFprobe (https://ffmpeg.org)
ExifTool (https://exiftool.org)

Step 1 - Backup
In case one of these conversions excludes the incorrect stream and you need to modify settings after checking and reprocess the file. This creates a backup of my entire library because I will eventually need to process most of it. I did this as a separate batch file.

ROBOCOPY "D:\TV Library" "E:\Workfile\MediaBackup\TV Library" *.* /S /E /XJD /R:25 /W:4 /NP /copyall /LOG:"C:\Tools\MediaTools\TV Log.txt"
ROBOCOPY "D:\Movie Library" "E:\Workfile\MediaBackup\Movie Library" *.* /S /E /XJD /R:25 /W:4 /NP /copyall /LOG:"C:\Tools\MediaTools\Movie Log.txt"

Step 2 - Change Drive & Directory to Source Files

D:
CD "D:\TV Library\Star Trek - The Original Series (1966)"

Step 3 - Analyze File Data

ffprobe "D:\TV Library\Star Trek - The Original Series (1966)\Season 01\S01E01 - The Man Trap.m4v"

In the output, look for Stream #. In my case a original (problem) file has the following stream data -
Stream 0:0 - AAC Audio
Stream 0:1 - AC3 Audio
Stream 0:2 - H.264 Video
Stream 0:3 - Subtitles
Stream 0:4 - Cover Art
Stream 0:5 - I think is metadata (but errors out with Unsupported codec with id 98314 for input stream 5)

Note: Even if the problem files are sourced from the same place, the stream order above might not be consistent. For example, another file in this grouping swapped Stream 0:1 and 0:2. Luckily for me, subtitles and metadata streams appear to be consistent in Stream 0:3 and 0:5.

Step 4 - Remux the file using FFmpeg

FOR /F "tokens=*" %%G IN ('dir /b /s *.m4v') DO ffmpeg.exe -i "%%G" -c copy -map 0 -map -0:3 -map -0:5 -movflags +faststart -map_metadata 0 "E:\Workfile\MediaFixed\%%~nG.m4v"

This command iterates thru the directory you changed to in Step 2 and chooses all files with the extension m4v (which is the format all my files have).
-c copy -map 0 - Copies all 0 based streams
-map -0:3 - Excludes Stream 3 (Subtitles)
-map -0:5 - Excludes Stream 5 (Metadata)
-movflags +faststart -map_metadata 0 - Copies most metadata (Note this option is only for MP4, M4A, M4V, MOV output.)

From my experience, you have to exclude the metadata stream with -map -0:5 because the ffmpeg encoder tries to find a metadata encoder, which isn’t valid.

Step 5 - Copy MetaData using ExifTool

FOR /F "tokens=*" %%G IN ('dir /b /s *.m4v') DO exiftool -tagsfromfile "%%G" "E:\Workfile\MediaFixed\%%~nG.m4v"

This command again, iterates thru all the source files and copies all tags to the remuxed files.

Step 6 - Remove Encoder Tag

FOR /F "tokens=*" %%G IN ('dir /b /s *.m4v') DO exiftool -encoder="" "%%G" "E:\Workfile\MediaFixed\%%~nG.m4v"

This is personal preference, but a great example how you can remove specific tags from a media file using ExifTool.

Step 7 - Change Drive & Directory to Remuxed Files

E:
CD "E:\Workfile\MediaFixed"

Step 8 - Delete Backups of remuxed files

DEL /S "E:\Workfile\MediaFixed\*.m4v_original"

ExifTool appears to take a backup of the file and places it in the same directory as the original with the .m4v_original extension. This is just a cleanup procedure. From further reading, if you include the -overwrite_original switch, it shouldn’t create a backup file. But I have not tested yet.

Step 9 - Analyze Remuxed Media
Either probe the remuxed files using ffprobe to ensure you have the streams you expect. Or watch the videos thru Plex and ensure you have everything you are expecting. After analyzing is complete and you are sure you have your media is proper, then you can delete the backups of your original problematic files.

Notes for Improvement of Script
I might eventually output the ffprobe data to a text file and analyze which streams to target for removal. Then I can remux based on an input file that tells me exactly which streams contain the subtitles and metadata to exclude.

I mean isn’t this easier to do with MKVtoolNix GUI then then convert back to MP4 with xMediaRecode’s MP4 Stream Copy?

If you prefer using CLI & Scripts I know KKVtoolNix is originally a CLI tool, I just really like the GUI, it’s super easy to use & xMediaRecode supports scripting, I just don’t know how much as it’s not something I use.