single.php

C# ffmpegを使ってURLから動画ファイルの解像度を取得する手順

C#で、.mp4 ファイルへのURLからFFmpegを使って動画の解像度を取得する手順について備忘録的に投稿しておきます。

ffprobeで取得

ffmpegをインストールで使える[ffprobe]で url を指定して取得ができます。

string? url = ".mp4 へのURL";

var psi = new ProcessStartInfo
{
    FileName = "ffprobe",
    Arguments = $"-v error -select_streams v:0 -show_entries stream=width,height -of csv=s=x:p=0 \"{url}\"",
    RedirectStandardOutput = true,
    RedirectStandardError = true,
    UseShellExecute = false,
    CreateNoWindow = true
};

Process? proc = Process.Start(psi);

if (proc == null) return;

string output = proc.StandardOutput.ReadToEnd();
proc.WaitForExit();

if (string.IsNullOrWhiteSpace(output)) return;

var parts = output.Trim().Split('x');
if (parts.Length != 2) return;

int width = int.Parse(parts[0]);
int height = int.Parse(parts[1]);

実行すると、動画の解像度が変数に保存されます。

まとめ

今回はC#で、.mp4 ファイルへのURLからFFmpegを使って動画の解像度を取得する手順につて書きました。

ffmpeg のインストールで使える[ffprobe]でURLから動画の解像度が取得可能です。

C#でURLから動画の解像度を取得したい人の参考になれば幸いです。

スポンサーリンク

最後までご覧いただき、ありがとうございます。

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です