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
'Python' 카테고리의 다른 글
Python: Declare variable in one line with list # 배열의 요소로 변수선언하기 (0) | 2023.04.01 |
---|---|
Python: rsplit # 파일 확장자 명 떼어서 이름 붙이기 (0) | 2023.03.28 |
isinstance() # check is instance of class # check type # datetime.date (0) | 2023.03.19 |
Python: Selenium in Linux # WSL # chromedriver # 예제 코드 (0) | 2023.03.06 |
이중 삼중 for문 break # forloop # break (0) | 2023.03.04 |