Skip to content

PlusMagi's Blog By Pitt Phunsanit

Plus emotional magic to the knowledge of logic.

  • About’s Pitt
Close Button

รวมความยาว videoรวมความยาว video

2010-08-082010-08-08| phunsanitphunsanit| 0 Comment | 07:00

ากต้องการรวมความยาวของวิดีโอทั้งหมดในโฟลเดอร์ เช่น /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
Read MoreRead More

Posts pagination

ก่อนหน้า 1 … 940 941 942 … 1,048 ถัดไป

Projects

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

Recent Posts

  • จัดคิวยังไงดี Redis / RabbitMQ
  • Angular: TabulatorPlus
  • Playwright: Environment
  • Vue.js: TabulatorPlus
  • AI MCP: Model Context Protocol

Archives

Categories

  • AI (21)
  • Businesses (6)
  • Design (43)
    • UX/UI (12)
  • DevOps (61)
    • CI/CD (4)
    • Docker (32)
    • GIT (25)
  • Histories (10)
  • Life (91)
    • Books (37)
    • Cartoon (3)
    • Sci-Fi (4)
    • Tips and Tricks (23)
    • พุทธ (5)
  • Network (122)
    • Apache HTTP Server (14)
    • IOT (1)
    • Nginx (28)
    • Stalwart (5)
  • Operating Systems (261)
    • Unix-like (189)
      • Android (15)
        • F-Droid (5)
      • iPhone (8)
      • Linux (112)
      • macOS (102)
        • Homebrew (17)
        • OrbStack (11)
      • Oh My ZSH (4)
      • Shell Script (34)
      • SSH (11)
    • Windows (117)
      • PowerShell (24)
      • WSL (24)
  • Programming (705)
    • .NET (18)
      • .NET Core EF (5)
      • C# (17)
    • API (26)
      • REST (5)
      • Swagger (6)
    • Database (180)
      • 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 (4)
    • Rust (1)
    • System Analyst (SA) (14)
    • Testing (17)
      • Automated Testing (9)
        • Playwright (6)
    • UML (7)
    • Web (372)
      • Backend (238)
        • Golang (1)
        • Java (45)
          • Spring Boot (14)
        • Node.js (1)
        • PHP (192)
          • Laravel (16)
          • Yii (5)
      • Frontend (128)
        • CSS (17)
          • Tailwind CSS (5)
        • JavaScript (116)
          • Angular (3)
          • jQuery (60)
          • Tabulator (15)
          • Vue.js (5)
      • WordPress (25)
  • Programs (86)
    • Excel (10)
  • Security (53)
  • Uncategorized (1)
  • กฎหมาย (6)

Sirat WordPress Theme By VWThemes

Scroll Up