www.geekybob.com

Just a short, simple blog for Bob to share his thoughts.

Fixing Underwater Videos with FFMPEG

23 September 2016 • by Bob • Batch Files, Photography, Scripting, FFMPEG

I ran into an interesting predicament: I couldn't get the right color adjustment settings to work in my video editor to correct some underwater videos from a scuba diving trip. After much trial and error, I came up with an alternative method: I have been able to successfully edit underwater photos to restore their color, so I used FFMPEG to export all of the frames from the source video as individual images, then I used a script to automate my photo editor to batch process all of the images, then I used FFMPEG to reassemble the finished results into a new MP4 file.

The following video of a Goliath Triggerfish in Bora Bora shows a before and after of what that looks like. Overall, I think the results are promising, albeit via a weird and somewhat time-consuming hack.

Exporting Videos as Images with FFMPEG

Here is the basic syntax for automating FFMPEG to export the individual frames:

ffmpeg.exe -i "input.mp4" -r 60 -s hd1080 "C:\path\%6d.png"

Where the following items are defined:

-i "input.mp4" specifies the source MP4 file
-r 60 specifies the frame rate for the video at 60fps
-s hd1080 specifies 1920x1080 resolution (there are others)
"C:\path\%6d.png" specifies the directory for storing the images, and specifies PNG images with file names which are numerically sequenced with a width of 6 digits (e.g. 000000.png to 999999.png)

Combining Images as a Video with FFMPEG

Here is the basic syntax for automating FFMPEG to combine the individual frames back into an MP4 file:

ffmpeg.exe -framerate 60 -i "C:\path\%6d.png" -c:v libx264 -f mp4 -pix_fmt yuv420p "output.mp4"

Where the following items are defined:

-framerate 60 specifies the frame rate for the output video at 60fps (note that specifying a different framerate than you used for exporting could be used to alter the playback speed of the final video)
-i "C:\path\%6d.png" specifies the directory where the images are stored, and specifies PNG images with file names which are numerically sequenced with a width of 6 digits (e.g. 000000.png to 999999.png)
-c:v libx264 specifies the H.264 codec
-f mp4 specifies an MP4 file
-pix_fmt yuv420p specifies the pixel format, which could also specify "rgb24" instead of "yuv420p"
"output.mp4" specifies the final MP4 file

How to Merge a Folder of MP4 Files with FFmpeg (Revisted)

06 July 2014 • by Bob • FFMPEG, Video Editing, Scripting, Batch Files

I ran into an interesting situation the other day: I had a bunch of H.264 MP4 files which I had created with Handbrake that I needed to combine, and I didn't want to use my normal video editor (Sony Vegas) to perform the merge. I'm a big fan of FFmpeg, so I figured that there was some way to automate the merge without having to use an editor.

I did some searching around the Internet, and I couldn't find anyone who was doing exactly what I was doing, so I wrote my own batch file that combines some tricks that I have used to automate FFmpeg in the past with some ideas that I found through some video hacking forums. Here is the resulting batch file, which will combine all of the MP4 files in a directory into a single MP4 file named "ffmpeg_merge.mp4", which can be renamed to something else:

@echo off

if exist ffmpeg_merge.mp4 del ffmpeg_merge.mp4
if exist ffmpeg_merge.tmp del ffmpeg_merge.tmp
if exist *.ts del *.ts

for /f "usebackq delims=|" %%a in (`dir /on /b *.mp4`) do (
ffmpeg.exe -i "%%a" -c copy -bsf h264_mp4toannexb -f mpegts "%%a.ts"
)

for /f "usebackq delims=|" %%a in (`dir /b *.ts`) do (
echo file %%a>>ffmpeg_merge.tmp
)

ffmpeg.exe -f concat -i ffmpeg_merge.tmp -c copy -bsf aac_adtstoasc ffmpeg_merge.mp4

if exist ffmpeg_merge.tmp del ffmpeg_merge.tmp
if exist *.ts del *.ts

The merging process in this batch file is performed in two steps:

  • First, all of the individual MP4 files are remuxed into individual transport streams
  • Second, all of the individual transport streams are remuxed into a merged MP4 file

Here are the URLs for the official documentation on each of the FFmpeg switches and parameters that I used:

By the way, I realize that there may be better ways to do this with FFmpeg, so I am open to suggestions. ;-]

Tag Cloud

11th-ACR 511th-MI-Company Access Add-ins ADSI Animals Antivaxxers AppCmd Apple Arizona Army Art ASP ASP.NET Atheism Authentication Bass Batch Files Bicycling BlogEngine.NET Blogging BSOD Business C# Campaigning Capitalism Cats Certificates Childhood Christianity Christmas Classic ASP Communism Computers Conspiracy Content Cookies COVID19 CPP Database Dell DIY Dogs Domain Administration Easter Ecology Education Effects Entertainment Environment Errors Essential-Tremor Exercise Expression Web Extensibility Facebook Family Fashion Feedback FFmpeg Filters Food FPSE Friends FrontPage FTP FTP Clients FTPS Gaming Germany GPO Grandparenting Guitar Halloween Hardware Hawaii Health History Hobbies Hotkeys HP HTML HTMLA HTTP HTTPS Humor IE IIS IIS 5 IIS 6 IIS 7 IIS 7.5 IIS 8 IIS 8.5 IIS Express Internet JavaScript Keystrokes Kickstarter Kids Languages LDAP Linguistics Log Files Log Parser LogParser Macros Marriage Math Media Membership Microsoft MIDI Military Miscellaneous Mods Movies Music Nature Nostalgia Office OneDrive Opinion Outlook PaintShop Parenthood Parody Peace Performance Philosophy Photography PHP Poetry Politics Ponderings PowerShell Programming Projects Proxy Puns PWS Random Thoughts Rants Religion Reviews RSCA Rush Russia Satire Science Scripting SDK Seattle Security SEO SharePoint Sibelius SkyDrive Socialism Space Sports Squirrels Squirrels, Humor SSI SSL Star Trek STEM Support Surface TechEd Technology Television Templates Thanksgiving Themes Transcription Travel Troubleshooting Tucson Upgrades URL URL Rewrite User Isolation VBA VBScript Video Visio Visual Basic Visual Studio VLC W3C Web API WebDAV WebMatrix Weight Loss WinCache Window Media Center Windows Windows 10 Windows Cache Extension Windows Phone 7 WMI Word Work Writing WSH XML XSL Zune