coursera.org から多数のビデオをダウンロードし、それらを 1 つの特定のフォルダーに保存しました。特定のフォルダーには多数の個別のビデオがあります (Coursera はレクチャーを複数の短いビデオに分割します)。特定のディレクトリ内のすべてのビデオの合計の長さを提供する python スクリプトが必要です。ビデオ ファイルは .mp4 形式です。
15446 次
5 に答える
7
まず、ffprobe
コマンド(FFmpegの一部)をインストールします
sudo apt install ffmpeg
次に、subprocess.run()
次の bash コマンドを実行します。
ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 -- <filename>
( http://trac.ffmpeg.org/wiki/FFprobeTips#Formatcontainerdurationから取得しました)、次のように:
from pathlib import Path
import subprocess
def video_length_seconds(filename):
result = subprocess.run(
[
"ffprobe",
"-v",
"error",
"-show_entries",
"format=duration",
"-of",
"default=noprint_wrappers=1:nokey=1",
"--",
filename,
],
capture_output=True,
text=True,
)
try:
return float(result.stdout)
except ValueError:
raise ValueError(result.stderr.rstrip("\n"))
# a single video
video_length_seconds('your_video.webm')
# all mp4 files in the current directory (seconds)
print(sum(video_length_seconds(f) for f in Path(".").glob("*.mp4")))
# all mp4 files in the current directory and all its subdirectories
# `rglob` instead of `glob`
print(sum(video_length_seconds(f) for f in Path(".").rglob("*.mp4")))
# all files in the current directory
print(sum(video_length_seconds(f) for f in Path(".").iterdir() if f.is_file()))
このコードにはPython 3.7+が必要です。その時点text=
で とcapture_output=
が に追加されたからsubprocess.run
です。古いバージョンの Python を使用している場合は、この回答の編集履歴を確認してください。
于 2014-09-15T07:05:49.370 に答える
3
- MediaInfoをダウンロードしてインストールします (バンドルされているアドウェアはインストールしないでください)。
- MediaInfo ソース ダウンロードに移動し、[ソースコード、すべて含まれています] 行で、[libmediainfo] の横にあるリンクを選択します。
MediaInfoDLL3.py
ダウンロードしたアーカイブを見つけて、任意の場所に展開します。場所の例:libmediainfo_0.7.62_AllInclusive.7z\MediaInfoLib\Source\MediaInfoDLL\MediaInfoDLL3.py
- 次に、テスト用のスクリプト (以下のソース) を同じディレクトリに作成します。
- スクリプトを実行します。
MediaInfo は POSIX でも動作します。唯一の違いはso
、DLL ではなく がロードされることです。
テストスクリプト (Python 3!)
import os
os.chdir(os.environ["PROGRAMFILES"] + "\\mediainfo")
from MediaInfoDLL3 import MediaInfo, Stream
MI = MediaInfo()
def get_lengths_in_milliseconds_of_directory(prefix):
for f in os.listdir(prefix):
MI.Open(prefix + f)
duration_string = MI.Get(Stream.Video, 0, "Duration")
try:
duration = int(duration_string)
yield duration
print("{} is {} milliseconds long".format(f, duration))
except ValueError:
print("{} ain't no media file!".format(f))
MI.Close()
print(sum(get_lengths_in_milliseconds_of_directory(os.environ["windir"] + "\\Performance\\WinSAT\\"
)), "milliseconds of content in total")
于 2013-02-23T17:38:31.697 に答える