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 … 1,060 1,061 1,062 … 1,168 ถัดไป

Projects

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

Recent Posts

  • PHP: การบีบอัด Output ด้วย Gzip/Deflate
  • SecDevOps: เปลี่ยนความปลอดภัยให้เป็นเนื้อเดียวกับ Code และ Operation
  • WordPress: ดูว่า post ทุก ๆ วันเสาร์ใช่มั๋ย
  • เทคนิคตั้งค่า umask 002 ให้กับทุก User บน Mac อัตโนมัติผ่าน Terminal (Batch Script)
  • TabulatorPlus: Deep Merge

Archives

Categories

  • AI (26)
  • Businesses (28)
  • Design (56)
    • UX/UI (22)
  • Histories (16)
  • Life (120)
    • Books (51)
    • Cartoon (6)
    • Movies (11)
    • Sci-Fi (20)
    • Tips and Tricks (26)
    • พุทธ (7)
  • Network (144)
    • Apache HTTP Server (17)
    • IOT (1)
    • Nginx (31)
    • Stalwart (6)
  • Operating Systems (279)
    • Unix-like (204)
      • Android (16)
        • F-Droid (5)
      • Homebrew (21)
      • iPhone (8)
      • Linux (119)
      • macOS (100)
        • OrbStack (11)
      • Oh My ZSH (4)
      • Shell Script (40)
      • SSH (11)
    • Windows (120)
      • PowerShell (25)
      • WSL (25)
  • Programming (786)
    • .NET (19)
      • .NET Core EF (6)
      • C# (17)
    • API (32)
      • REST (6)
      • Swagger (6)
    • Database (187)
      • DBeaver (3)
      • MariaDB (31)
      • MySql (68)
      • Oracle Database (6)
        • 10g (3)
      • PostgreSQL (9)
      • RDBMS (2)
      • SQL Server (85)
        • SSMS (8)
        • T-SQL (26)
      • SQLite (1)
    • PowerBuilder (10)
    • Python (5)
    • Rust (4)
    • System Analyst (SA) (37)
    • Testing (21)
      • Automated Testing (12)
        • Playwright (6)
    • UML (7)
    • Web (414)
      • Backend (270)
        • Golang (1)
        • Java (47)
          • Spring Boot (14)
        • Node.js (2)
        • PHP (221)
          • Laravel (50)
          • Yii (5)
      • Frontend (134)
        • CSS (18)
          • Tailwind CSS (5)
        • JavaScript (121)
          • Angular (3)
          • jQuery (61)
          • Tabulator (19)
          • Vue.js (5)
      • WordPress (26)
  • Programs (97)
    • Excel (12)
  • SecDevOps (93)
    • CI/CD (8)
    • Docker (41)
    • GIT (32)
  • Security (81)
  • Uncategorized (3)
  • กฎหมาย (9)

Sirat WordPress Theme By VWThemes

Scroll Up
Exit mobile version