如何基于文件名时间模式生成指定时段的监控视频文件列表
Hey there, let's figure out how to get that filtered list of your camera videos quickly. Based on your file structure and the time range you care about (8:00 AM to 5:00 PM), brace expansion is exactly the right tool here—super efficient for matching patterns like this.
First, let's recap your setup to make sure we're on the same page:
Your video path format:
[Date Folder]/AM|PM/sricam-[date]-[time]-sequence#.mp4
Goal: Only include videos recorded between 8:00 AM and 17:00 (so AM hours 8-12, PM hours 1-5, since PM 1 = 13:00, PM 5 = 17:00)
Using Brace Expansion to List Valid Videos
Here's a bash command that uses brace expansion to target exactly those time slots. Run this in the parent directory that holds all your date folders:
ls */{AM/sricam-*-0{8,9,10,11,12}-*.mp4,PM/sricam-*-0{1,2,3,4,5}-*.mp4}
Breakdown of the command:
*/matches every top-level date folder (so you don't have to specify each date manually)- The outer
{AM/..., PM/...}lets us target both AM and PM subfolders in one goAM/sricam-*-0{8,9,10,11,12}-*.mp4grabs all AM videos from 8:00 to 12:00—0{8,9,10,11,12}expands to08,09,10,11,12to match the hour part of the filenamePM/sricam-*-0{1,2,3,4,5}-*.mp4captures PM videos from 1:00 to 5:00 (which maps to 13:00-17:00 in 24-hour time)
If you want to save this list to a text file instead of just printing it to the terminal, add a redirect:
ls */{AM/sricam-*-0{8,9,10,11,12}-*.mp4,PM/sricam-*-0{1,2,3,4,5}-*.mp4} > valid_camera_videos.txt
Quick Notes:
- If your filenames include minutes (like
0830for 8:30 AM), adjust the pattern to08*instead of08to capture all times starting with that hour. For example:AM/sricam-*-08*-*.mp4 - This relies on shell brace expansion, which works in bash, zsh, and most modern shells—if you're using an older shell, this trick won't apply.
内容的提问来源于stack exchange,提问作者Max

