Skip to content

Instantly share code, notes, and snippets.

@priintpar
Last active February 5, 2025 01:51
Show Gist options
  • Save priintpar/f7a56af8977206e9f45b486f767b02ac to your computer and use it in GitHub Desktop.
Save priintpar/f7a56af8977206e9f45b486f767b02ac to your computer and use it in GitHub Desktop.
Convert many .media files created by security cameras and babyphones to a single .mkv file using ffmpeg on Macos or Linux. Even skips defective files.
find . -type f -name '*media' >> unsortedlist.txt
sort unsortedlist.txt >> sortedlist.txt
while read line; do ffprobe $line; if [ $? = "1" ]; then echo $line; fi; done < sortedlist.txt >> corruptfiles.txt
awk 'NR==FNR{a[$0];next} !($0 in a)' corruptfiles.txt sortedlist.txt > temp; mv temp sortedlist.txt
while read line; do echo "file '$line'"; done < sortedlist.txt >> sortedlistwithoutcorruptfiles.txt
ffmpeg -f concat -safe 0 -i sortedlistwithoutcorruptfiles.txt -c copy outputvideo.mkv
rm unsortedlist.txt sortedlist.txt sortedlistwithoutcorruptfiles.txt corruptfiles.txt
@Darex1991
Copy link

Nice :) Really thank you for that!

@Gast114
Copy link

Gast114 commented Nov 19, 2023

Has anyone been able to solve the issue with audio not being detected? I can merge the files into one, but the audio is missing. I've tried everything possible with ffmpeg and similar tools, but with no success. Here is a sample file if anyone wants to have a look. Oh, and here is the script for converting it on Windows with PowerShell.

https://user-images.githubusercontent.com/89197752/284089641-01ebbc7a-6459-4858-a256-1ba0f5e9dd8a.mp4
You need to remove the .mp4 at the end and change it to .media.

# Find all .media files and sort them, then store them in sortedlist.txt
Get-ChildItem -Recurse -Filter "*.media" | Sort-Object Name | Select-Object -ExpandProperty FullName > sortedlist.txt

# Initialize an empty list to store corrupt files
$corruptFiles = @()

# Check each file with ffprobe and add to the corrupt files list if an error occurs
Get-Content sortedlist.txt | ForEach-Object {
    $file = $_
    $ffprobeOutput = & ffprobe $file 2>&1
    if ($LASTEXITCODE -ne 0) {
        $corruptFiles += $file
    }
}

# Write corrupt files to corruptfiles.txt
$corruptFiles | Out-File corruptfiles.txt

# Remove corrupt files from the sorted list
$sortedFiles = Get-ChildItem -Recurse -Filter "*.media" | Sort-Object Name | Select-Object -ExpandProperty FullName

# Prepare the file list for ffmpeg
$sortedFiles | ForEach-Object { "file '$_'" } | Out-File -Encoding ASCII sortedlistwithoutcorruptfiles.txt

# Use ffmpeg to concatenate the non-corrupt files into a single video file
& ffmpeg -f concat -safe 0 -i sortedlistwithoutcorruptfiles.txt -c copy outputvideo.mkv

# Remove temporary files
$filesToRemove = @('unsortedlist.txt', 'sortedlist.txt', 'sortedlistwithoutcorruptfiles.txt', 'corruptfiles.txt')
foreach ($file in $filesToRemove) {
    $path = Join-Path -Path (Get-Location) -ChildPath $file
    if (Test-Path $path) {
        Remove-Item $path
    }
}

@Darex1991
Copy link

Darex1991 commented Nov 19, 2023

You need to buy mac :trollface:

@Gast114
Copy link

Gast114 commented Nov 19, 2023

I'm curious if you were actually able to convert the .media file while retaining the audio, and if you could send it back to me. If this works, I would consider borrowing a Mac to perform the conversions instead of purchasing one.

@Darex1991
Copy link

Video link is not working for me. There is player but without video

@sambul13
Copy link

sambul13 commented Feb 5, 2025

The convertion code doesn't work when embedded audio is encoded in NHNT format typical for cloud cameras, but not usually recognised by default config ffprobe.

You need to buy mac

Irrelevant for this error. Further, the code purges present in the media folder structure "non-media" files with audio codec info, and fails to identify in the targeted day folder structure correct start & sequence & stop fragments belonging to the same motion event video. Too simplistic to be universally useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment