วิธีการคำนวณเวลาของวีดีโอในภาษา python

          ในภาษา Python มีวีธีการคำนวณเวลา โดยจะการใช้ Json เข้ามาเกี่ยวข้องด้วยโดยจะเก็บข้อมูลต่าง แล้วจะมี library ที่ชื่อว่า subprocess เกี่ยวข้อง โดยการคำนวณเวลาในไฟล์เวลานั้นจะต้องทำการเรียกใช้หรือทำการ import เข้ามาใช้งานก่อนถึงจะทำงานได้

ก่อนที่จะไปเขียนมารู้จักความหมายพวกนี้กันก่อน

          JSON (JavaScript Object Notation) รูปแบบการรับส่งข้อมูลคอมพิเตอร์ขนาดเล็ก เป็นภาษาที่อ่าน, เขียน และสร้างเป็นภาษาโปรแกรมที่เข้าใจง่าย ถูกสร้างขึ้นมาโดย ดักกลาส คลอฟอร์ด ต้นกำเนิดอยู่ในรูปแบบ ของ RFC 4627 and ECMA-404 JSON เป็นรูปแบบข้อความภาษาอิสระ

ขั้นตอนการเขียน

ให้ทำการ import library module ก่อน

import subprocess as sp
import json

จากนั้นให้สร้างฟังก์ชั่น probe

def probe(vid_file_path):

    if type(vid_file_path) != str:
        raise Exception('Gvie ffprobe a full file path of the video')
        return

    command = ["ffprobe",
            "-loglevel",  "quiet",
            "-print_format", "json",
             "-show_format",
             "-show_streams",
             vid_file_path
             ]

    pipe = sp.Popen(command, stdout=sp.PIPE, stderr=sp.STDOUT)
    out, err = pipe.communicate()
    return json.loads(out)

สร้างฟังก์ชั่น duration เพื่อไว้สำหรับหาระยะเวลาของวีดีโอ

def duration(vid_file_path):
    _json = probe(vid_file_path)

    if 'format' in _json:
        if 'duration' in _json['format']:
            return float(_json['format']['duration'])

    if 'streams' in _json:
        # commonly stream 0 is the video
        for s in _json['streams']:
            if 'duration' in s:
                return float(s['duration'])

จากนั้นทำการเรียกใช้ฟังก์ชั่น duration

# -*- coding=utf-8 -*-

import subprocess as sp
import json

def probe(vid_file_path):

    if type(vid_file_path) != str:
        raise Exception('Gvie ffprobe a full file path of the video')
        return

    command = ["ffprobe",
            "-loglevel",  "quiet",
            "-print_format", "json",
             "-show_format",
             "-show_streams",
             vid_file_path
             ]

    pipe = sp.Popen(command, stdout=sp.PIPE, stderr=sp.STDOUT)
    out, err = pipe.communicate()
    return json.loads(out)

def duration(vid_file_path):
    _json = probe(vid_file_path)

    if 'format' in _json:
        if 'duration' in _json['format']:
            return float(_json['format']['duration'])

    if 'streams' in _json:
        # commonly stream 0 is the video
        for s in _json['streams']:
            if 'duration' in s:
                return float(s['duration'])

time_vdo= duration('content2language.mp4') # pathของวีดีโอ
end_minute=int(time_vdo/60)
end_second = int(time_vdo%60)
print 'นาที : %d' %end_minute
print 'วินาที : %d' %end_second

 ผลที่ได้ก็จะเป็น นาทีและ วินาทีของวีดีโอ ก็ทำอันที่เรียบร้อย

 

 

 

อ้างอิง

stackoverflow.(2551).How to get the duration of a video in Python?.[ออนไลน์].เข้าถึงได้จาก https://stackoverflow.com/questions/3844430/how-to-get-the-duration-of-a-video-in-python. [19 ส.ค. 2562]

 

กระทู้ล่าสุดจากเว็บบอร์ด
หัวข้อกระทู้
ตอบ
เปิดดู
ล่าสุด
คำศัพท์ Antonyms ที่ใช้ในชีวิตประจำวัน
โดย internTk21 จ 08 เม.ย. 2024 11:52 am บอร์ด Microsoft Office Knowledge & line & Etc
0
135
จ 08 เม.ย. 2024 11:52 am โดย internTk21 View Topic คำศัพท์ Antonyms ที่ใช้ในชีวิตประจำวัน
Stress คำศัพท์ภาษาอังกฤษอย่างไรให้ถูกต้อง
โดย internTk21 อ 07 เม.ย. 2024 11:57 pm บอร์ด Microsoft Office Knowledge & line & Etc
0
138
อ 07 เม.ย. 2024 11:57 pm โดย internTk21 View Topic Stress คำศัพท์ภาษาอังกฤษอย่างไรให้ถูกต้อง
การใช้ Intonation ในภาษาอังกฤษ
โดย internTk21 ส 06 เม.ย. 2024 1:46 pm บอร์ด Microsoft Office Knowledge & line & Etc
0
170
ส 06 เม.ย. 2024 1:46 pm โดย internTk21 View Topic การใช้ Intonation ในภาษาอังกฤษ
พื้นฐานการทำ SEO ในเว็บไซต์ เส้นทางสู่การเพิ่มประสิทธิภาพในการค้นหา
โดย athirach.offcial พฤ 04 เม.ย. 2024 4:53 pm บอร์ด Share Knowledge
0
276
พฤ 04 เม.ย. 2024 4:53 pm โดย athirach.offcial View Topic พื้นฐานการทำ SEO ในเว็บไซต์ เส้นทางสู่การเพิ่มประสิทธิภาพในการค้นหา
ลักษณะการทดสอบโปรแกรมที่ไม่เหมาะกับการทดสอบอัตโนมัติ
โดย athirach.offcial พฤ 04 เม.ย. 2024 4:49 pm บอร์ด Software testing
0
297
พฤ 04 เม.ย. 2024 4:49 pm โดย athirach.offcial View Topic ลักษณะการทดสอบโปรแกรมที่ไม่เหมาะกับการทดสอบอัตโนมัติ
Tester trick ข้อควรระวังในการทดสอบโปรแกรม
โดย athirach.offcial พฤ 04 เม.ย. 2024 4:43 pm บอร์ด Software testing
0
100
พฤ 04 เม.ย. 2024 4:43 pm โดย athirach.offcial View Topic Tester trick ข้อควรระวังในการทดสอบโปรแกรม
การทดสอบและบทบาทของผู้ทดสอบโปรแกรมอัตโนมัติในปี 2024
โดย athirach.offcial พฤ 04 เม.ย. 2024 4:38 pm บอร์ด Software testing
0
89
พฤ 04 เม.ย. 2024 4:38 pm โดย athirach.offcial View Topic การทดสอบและบทบาทของผู้ทดสอบโปรแกรมอัตโนมัติในปี 2024
ตัวอย่าง Prefix และ Suffix ในภาษาอังกฤษ
โดย internTk21 พฤ 04 เม.ย. 2024 4:34 pm บอร์ด Microsoft Office Knowledge & line & Etc
0
164
พฤ 04 เม.ย. 2024 4:34 pm โดย internTk21 View Topic ตัวอย่าง Prefix และ Suffix ในภาษาอังกฤษ