<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>FFmpeg on Toothbrush's Blog</title><link>https://www.tianhao.me/en/tags/ffmpeg/</link><description>Recent content in FFmpeg on Toothbrush's Blog</description><generator>Hugo -- gohugo.io</generator><language>en</language><copyright>牙刷刷</copyright><lastBuildDate>Sat, 12 Jul 2025 17:10:27 +0800</lastBuildDate><atom:link href="https://www.tianhao.me/en/tags/ffmpeg/index.xml" rel="self" type="application/rss+xml"/><item><title>My Smart Assistant 3.0: Building a Fully Automated "Time Screening Room"</title><link>https://www.tianhao.me/en/posts/rk-camera-capture-merge/</link><pubDate>Sat, 12 Jul 2025 17:10:27 +0800</pubDate><guid>https://www.tianhao.me/en/posts/rk-camera-capture-merge/</guid><description>&lt;img src="https://www.tianhao.me/img/avatar.png" alt="Featured image of post My Smart Assistant 3.0: Building a Fully Automated "Time Screening Room"" /&gt;&lt;p&gt;Welcome back to my &amp;ldquo;lazy&amp;rdquo; photography system series. In the previous chapters, we witnessed how an ordinary webcam was endowed with the ability to &amp;ldquo;think,&amp;rdquo; and how I built an elegant and convenient &amp;ldquo;control room&amp;rdquo; for it. However, the story doesn&amp;rsquo;t end there. Our clever photographer has been smartly &amp;ldquo;slacking off&amp;rdquo; every day, accurately capturing a vast number of valuable moments. These photos, like scattered pearls, contain the secrets of time, quietly resting deep within the server&amp;rsquo;s hard drive.&lt;/p&gt;&#10;&lt;p&gt;It&amp;rsquo;s time to string these pearls together, letting them shine brilliantly through the flow of light and shadow. Today, I will show you the final chapter of this project, which I personally find the most exciting — building a fully automated &amp;ldquo;Time Screening Room.&amp;rdquo; At the core of this screening room is a powerful and reliable shell script. It is the &amp;ldquo;behind-the-scenes director&amp;rdquo; of my entire system, entrusted with a sacred mission: before each quiet dawn arrives, automatically merging tens of thousands of photos from the previous day into wonderful time-lapse videos.&lt;/p&gt;&#10;&lt;p&gt;Project code: &lt;a class="link" href="https://github.com/AndroidOL/camera-capture/tree/main/merge" target="_blank" rel="noopener"&#10; &gt;https://github.com/AndroidOL/camera-capture/tree/main/merge&lt;/a&gt;&lt;/p&gt;&#10;&lt;h2 id="from-material-pileup-to-visual-poems-the-final-billion-step"&gt;From &amp;ldquo;Material Pileup&amp;rdquo; to &amp;ldquo;Visual Poems&amp;rdquo;: The Final &amp;ldquo;Billion&amp;rdquo; Step&#10;&lt;/h2&gt;&lt;p&gt;Imagine what life would be like without this automated &amp;ldquo;director.&amp;rdquo; It would mean that every day, I’d have to act like a &amp;ldquo;miner&amp;rdquo; in the digital age — manually logging into a remote server to dig through tens of thousands of photos from the previous day, which might amount to dozens of gigabytes; then carefully downloading these &amp;ldquo;raw ores&amp;rdquo; to my local computer; next, launching complex video editing software, painstakingly dragging each photo onto the timeline, setting frame rates, encoders, bitrates&amp;hellip; and finally clicking &amp;ldquo;render&amp;rdquo; after a long wait.&lt;/p&gt;&#10;&lt;p&gt;This entire process is not only a battle of patience and endurance with no creativity but also prone to errors. If one day I forget or get too busy at work and miss it, the &amp;ldquo;visual diary&amp;rdquo; for that day will have an irreplaceable gap. That&amp;rsquo;s not what I want.&lt;/p&gt;&#10;&lt;p&gt;What I need is a fully unattended, extremely stable and reliable, and smart enough automated process. It should be a tireless artist that quietly starts at a fixed time every day, for example, in the dead of night, and like a skilled film editor, carefully arranges the scattered materials of the previous day into a complete film, then elegantly places it on the digital shelf of the &amp;ldquo;family film library,&amp;rdquo; waiting for my review.&lt;/p&gt;&#10;&lt;p&gt;Thus, this labor of love — the automation script &lt;code&gt;daily_photo_to_video.sh&lt;/code&gt; — was born. It’s far from a simple ffmpeg command wrapper; it is an industrial-grade robust &amp;ldquo;smart production line.&amp;rdquo;&lt;/p&gt;&#10;&lt;h2 id="the-directors-extraordinary-skills-revealing-the-core-design-of-the-automation-script"&gt;The Director&amp;rsquo;s Extraordinary Skills: Revealing the Core Design of the Automation Script&#10;&lt;/h2&gt;&lt;p&gt;The design philosophy of this script is &lt;strong&gt;&amp;ldquo;pursue ultimate stability and efficiency.&amp;rdquo;&lt;/strong&gt; I armed it to the teeth so that it can calmly handle all foreseeable and even unforeseeable complex situations.&lt;/p&gt;&#10;&lt;h3 id="1-ultimate-efficiency-squeezing-every-drop-of-hardware-potential"&gt;1. Ultimate Efficiency: Squeezing Every Drop of Hardware Potential&#10;&lt;/h3&gt;&lt;p&gt;&lt;strong&gt;Highway — Ramdisk (In-Memory Disk)&lt;/strong&gt;&lt;br&gt;&#10;Reading tens of thousands of scattered small images on a traditional hard drive while writing a huge video file immediately exposes disk I/O bottlenecks, making the whole process painfully slow. My solution is radical: directly carve out a high-speed virtual disk (Ramdisk) in the server’s precious memory.&lt;/p&gt;&#10;&lt;p&gt;When the script starts, it first synchronizes all photos to be processed from the photo server to this memory disk via high-speed network. Then, all intensive read-write operations of video synthesis (encoding) are performed entirely on this virtual disk. Memory read/write speeds are tens or even hundreds of times faster than ordinary hard drives, which is like building a &amp;ldquo;star highway&amp;rdquo; that ignores physical laws and travels at light speed for data migration, greatly shortening processing time.&lt;/p&gt;&#10;&lt;p&gt;&lt;strong&gt;Professional Engine — Hardware Encoding (HEVC_RKMPP)&lt;/strong&gt;&lt;br&gt;&#10;Video encoding is a typical CPU-intensive task. Relying solely on the CPU to &amp;ldquo;hard compute&amp;rdquo; is like asking a versatile but tired butler to do a highly specialized woodworking job — inefficient and causing the server’s &amp;ldquo;heartbeat&amp;rdquo; (CPU usage) to spike and the fans to roar. Fortunately, the server I use (based on Rockchip chips) has a dedicated video processing unit (VPU).&lt;/p&gt;&#10;&lt;p&gt;My script precisely calls ffmpeg and specifies the use of the &lt;code&gt;hevc_rkmpp&lt;/code&gt; hardware encoder. This is equivalent to handing the professional work to a specially hired, highly skilled &amp;ldquo;film editor,&amp;rdquo; letting the VPU handle the heaviest encoding tasks, fully freeing the CPU. Meanwhile, I choose the efficient HEVC (H.265) encoding format, which maintains very high picture quality while compressing the video size to about half of traditional H.264, saving massive precious space in my &amp;ldquo;family film library.&amp;rdquo;&lt;/p&gt;&#10;&lt;h3 id="2-foundation-of-stability-industrial-grade-fault-tolerance-and-self-healing"&gt;2. Foundation of Stability: Industrial-Grade Fault Tolerance and Self-Healing&#10;&lt;/h3&gt;&lt;p&gt;A spaceship sailing alone in the wilderness values not only speed but also its ability to survive cosmic storms. Likewise, a script that needs to run unattended for years requires strong resilience. I built four layers of &amp;ldquo;security protocols&amp;rdquo; to handle various possible accidents.&lt;/p&gt;&#10;&lt;table&gt;&#10;&#9;&lt;thead&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;th&gt;Scenario&lt;/th&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;th&gt;Script’s Smart Response Strategy&lt;/th&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&lt;/thead&gt;&#10;&#9;&lt;tbody&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;Unexpected script interruption&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;Uses &lt;code&gt;trap&lt;/code&gt; cleanup mechanism to automatically clear temp files and unmount Ramdisk on interruption&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;Concurrent run conflicts&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;Uses &lt;code&gt;flock&lt;/code&gt; file lock to avoid conflicts from multiple instances running concurrently&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;Insufficient storage space&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;Pre-checks available space in target path to avoid write failures midway&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;Target disk disconnection&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;Automatically switches to backup storage path, ensures task completion, and pauses old task catch-up&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&lt;/tbody&gt;&#10;&lt;/table&gt;&#10;&lt;h3 id="3-smart-scheduling-never-miss-a-days-highlights"&gt;3. Smart Scheduling: Never Miss a Day’s Highlights&#10;&lt;/h3&gt;&lt;p&gt;I want every day’s video in my &amp;ldquo;screening room&amp;rdquo; to be complete, like a coherent chronicle. Therefore, the smartest and proudest part of the script is its almost obsessive &amp;ldquo;completion&amp;rdquo; logic.&lt;/p&gt;&#10;&lt;p&gt;When running, the script:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;Reviews logs from the past 7 days, checking for missing or failed videos;&lt;/li&gt;&#10;&lt;li&gt;If omissions are found, it prioritizes filling gaps starting from the earliest day;&lt;/li&gt;&#10;&lt;li&gt;Verifies that remote materials still exist to avoid pointless runs;&lt;/li&gt;&#10;&lt;li&gt;Only after confirming completeness does it process &amp;ldquo;yesterday’s&amp;rdquo; new materials.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;This way, the system gains memory and responsibility, always maintaining a complete &amp;ldquo;visual diary.&amp;rdquo;&lt;/p&gt;&#10;&lt;h2 id="the-birth-of-a-family-epic"&gt;The Birth of a &amp;ldquo;Family Epic&amp;rdquo;&#10;&lt;/h2&gt;&lt;p&gt;Now, this has become a ritual-filled daily routine. Every morning, when I brew coffee and open the &amp;ldquo;family film library,&amp;rdquo; a brand-new time-lapse video named after yesterday’s date quietly rests there.&lt;/p&gt;&#10;&lt;p&gt;It might capture the entire thunderstorm outside the window, or my figure working late into the night, or simply portray the traces of sunlight flowing through the home.&lt;/p&gt;&#10;&lt;p&gt;This automated script is like a silent but powerful heart, injecting endless vitality into my entire smart photography project. It transforms day-after-day trivial visual data into distilled visual poems full of time’s essence, turning cold data into warm memories, allowing me to truly &amp;ldquo;see&amp;rdquo; and &amp;ldquo;cherish&amp;rdquo; those silently passing moments.&lt;/p&gt;&#10;&lt;h2 id="the-joy-of-creation"&gt;The Joy of Creation&#10;&lt;/h2&gt;&lt;p&gt;The joy of creation lies here — using code and logic to build a system that automatically creates, organizes, and presents beauty for us.&lt;/p&gt;&#10;&lt;p&gt;Next steps might be training an AI to automatically add the most fitting background music to these videos, or editing &amp;ldquo;weekly highlights.&amp;rdquo;&lt;/p&gt;&#10;&lt;p&gt;The journey to a &amp;ldquo;better life&amp;rdquo; through creation is endless, and that is its true charm.&lt;/p&gt;&#10;</description></item></channel></rss>