본문 바로가기

Python

Python: moviepy # duration # 비디오 파일 재생시간 가져오기

728x90
반응형

Python

moviepy library

 

To get the total playing time of a video in Django, you can use the moviepy library, which is a powerful library for video editing in Python. First, you need to install the library using pip:

pip install moviepy

 

Then, you can use the VideoFileClip class from the moviepy.editor module to read the video file and obtain its duration:

 

from moviepy.editor import VideoFileClip

def get_video_duration(video_path):
    with VideoFileClip(video_path) as clip:
        duration = clip.duration
    return duration

# Usage example
video_path = "path/to/your/video/file.mp4"
duration = get_video_duration(video_path)
print(f"Video duration: {duration} seconds")

This function reads the video file specified by video_path and returns its duration in seconds. The with statement is used to automatically close the video file after obtaining the duration, which is a good practice to avoid resource leaks.

In your Django application, you can call this function with the file path stored in the FilePathField to get the duration of the uploaded video.

 

주의 - Django application이 동작하는 서버에 비디오가 저장되어 있어야 접근 가능, remote sroage service일 경우 불가 (다운로드 받던가 해야함)

Note that this approach requires the video file to be stored on the same server as the Django application. If you're using a remote storage service, you may need to download the video file to a temporary location before processing it with moviepy

728x90
반응형