Skip to content

PlusMagi's Blog By Pitt Phunsanit

Plus emotional magic to the knowledge of logic.

  • About’s Pitt
Close Button
PlusMagi's Blog By Pitt Phunsanit macOS,Python,Shell Script,Unix-like รวมความยาว video

รวมความยาว video

2010-08-082010-08-08| phunsanitphunsanit| 0 Comment | 07:00
Categories:
  • macOS
  • Python
  • Shell Script
  • Unix-like

ากต้องการรวมความยาวของวิดีโอทั้งหมดในโฟลเดอร์ เช่น /Users/Shared/meetings บน macOS คุณสามารถใช้คำสั่งบน Terminal ได้หลายวิธีครับ โดยวิธีที่แม่นยำที่สุดคือการใช้ ffprobe (ซึ่งมาพร้อมกับ ffmpeg) หรือจะใช้คำสั่ง mdls ที่มีอยู่แล้วในเครื่อง macOS ก็ได้ครับ


ใช้ mdls (สะดวกที่สุด ไม่ต้องติดตั้งโปรแกรมเพิ่ม)

macOS มีระบบ Spotlight คอยเก็บ Metadata ของไฟล์อยู่แล้ว สามารถรันคำสั่งนี้บน Terminal เพื่อหาความยาวรวม (วินาที) แล้วแปลงเป็น ชั่วโมง:นาที:วินาที ได้ทันที
find /Users/Shared/meetings -type f ( -name ".mp4" -o -name ".mov" -o -name "*.mkv" ) -print0 | xargs -0 mdls -name kMDItemDurationSeconds | awk '{sum += $3} END {printf "%02d:%02d:%02d\n", sum/3600, (sum%3600)/60, sum%60}'
หมายเหตุ: สามารถเพิ่มหรือลดนามสกุลไฟล์วิดีโอในเครื่องหมาย \( ... \) ได้ตามต้องการ เช่น -name "*.avi"


ใช้ ffprobe (แม่นยำที่สุด สำหรับสาย Dev)

หากในเครื่องมี ffmpeg ติดตั้งอยู่แล้ว (เช่น ติดตั้งผ่าน Homebrew) วิธีนี้จะเช็กจากตัวไฟล์วิดีโอโดยตรงและแม่นยำมากครับ
find /Users/Shared/meetings -type f ( -name ".mp4" -o -name ".mov" -o -name "*.mkv" ) -print0 | xargs -0 -I {} ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "{}" | awk '{sum += $1} END {printf "%02d:%02d:%02d\n", sum/3600, (sum%3600)/60, sum%60}'


ใช้สคริปต์ Python (อ่านง่ายและปรับแต่งต่อสะดวก)

หากต้องการผลลัพธ์ที่ละเอียดขึ้น สามารถสร้างไฟล์สคริปต์ Python สั้น ๆ (ใช้ตัวอ่าน metadata พื้นฐานของ macOS) รันคำสั่งนี้ใน Terminal ได้เลยครับ

python3 -c '
import os, subprocess
total_seconds = 0
path = "/Users/Shared/meetings"
extensions = (".mp4", ".mov", ".mkv", ".avi")

for root, dirs, files in os.walk(path):
    for file in files:
        if file.lower().endswith(extensions):
            filepath = os.path.join(root, file)
            try:
                # ดึงเวลาเป็นวินาทีผ่าน mdls
                output = subprocess.check_output(f"mdls -name kMDItemDurationSeconds \"{filepath}\"", shell=True).decode()
                if "kMDItemDurationSeconds =" in output:
                    sec = float(output.split("=")[1].strip())
                    total_seconds += sec
            except:
                pass

hours = int(total_seconds // 3600)
minutes = int((total_seconds % 3600) // 60)
seconds = int(total_seconds % 60)
print(f"เวลาทั้งหมด: {hours} ชั่วโมง {minutes} นาที {seconds} วินาที (รวม {total_seconds:.2f} วินาที)")
'

อ่านเพิ่มเติม

  • VLC Media Player: ดู video ได้ทุกเรื่อง ทุกไฟล์
  • ดูดโหลด video, MP3 โดย yt-dlp
  • yt-dlp: Options
ป้ายกำกับ:avi, ffmpeg, ffprobe, macOS, mdls, Metadata, mkv, mov, mp4, Python, Spotlight, Sum, Terminal, Video, ความยาว, นามสกุล, รวม, วิดีโอ, เก็บ, โฟลเดอร์, ไฟล์

แนะแนวเรื่อง

PREVIOUS Previous post: แก้ปัญหา MySql ไม่ทำงาน
NEXT Next post: อีกข้อดีของ jQuery

Projects

  • Statement Columns Mapping Helper
  • PlusMagi Site Search
  • jQuery Plus Repeater

Recent Posts

  • Playwright: Environment
  • Vue.js: TabulatorPlus
  • AI MCP: Model Context Protocol
  • React: TabulatorPlus
  • SonarQube: ติดตั้ง

Archives

Categories

  • AI (16)
  • Businesses (4)
  • Design (43)
    • UX/UI (12)
  • DevOps (61)
    • CI/CD (4)
    • Docker (32)
    • GIT (25)
  • Histories (10)
  • Life (84)
    • Books (30)
    • Cartoon (3)
    • Sci-Fi (4)
    • Tips and Tricks (22)
    • พุทธ (5)
  • Network (119)
    • Apache HTTP Server (14)
    • IOT (1)
    • Nginx (28)
    • Stalwart (5)
  • Operating Systems (258)
    • Unix-like (186)
      • Android (15)
        • F-Droid (5)
      • iPhone (8)
      • Linux (112)
      • macOS (99)
        • Homebrew (15)
        • OrbStack (11)
      • Oh My ZSH (4)
      • Shell Script (34)
      • SSH (11)
    • Windows (116)
      • PowerShell (24)
      • WSL (24)
  • Programming (692)
    • .NET (18)
      • .NET Core EF (5)
      • C# (17)
    • API (25)
      • REST (5)
      • Swagger (6)
    • Database (177)
      • DBeaver (3)
      • MariaDB (28)
      • MySql (66)
      • Oracle Database (6)
        • 10g (3)
      • PostgreSQL (8)
      • RDBMS (2)
      • SQL Server (85)
        • SSMS (8)
        • T-SQL (26)
      • SQLite (1)
    • PowerBuilder (10)
    • Python (2)
    • Rust (1)
    • System Analyst (SA) (9)
    • Testing (17)
      • Automated Testing (9)
        • Playwright (6)
    • UML (7)
    • Web (369)
      • Backend (238)
        • Golang (1)
        • Java (45)
          • Spring Boot (14)
        • Node.js (1)
        • PHP (192)
          • Laravel (16)
          • Yii (5)
      • Frontend (127)
        • CSS (17)
          • Tailwind CSS (5)
        • JavaScript (115)
          • Angular (2)
          • jQuery (60)
          • Tabulator (14)
          • Vue.js (5)
      • WordPress (25)
  • Programs (84)
    • Excel (10)
  • Security (52)
  • Uncategorized (1)
  • กฎหมาย (6)

Sirat WordPress Theme By VWThemes

Scroll Up
Go to mobile version