banner
andrewji8

Being towards death

Heed not to the tree-rustling and leaf-lashing rain, Why not stroll along, whistle and sing under its rein. Lighter and better suited than horses are straw sandals and a bamboo staff, Who's afraid? A palm-leaf plaited cape provides enough to misty weather in life sustain. A thorny spring breeze sobers up the spirit, I feel a slight chill, The setting sun over the mountain offers greetings still. Looking back over the bleak passage survived, The return in time Shall not be affected by windswept rain or shine.
telegram
twitter
github

Pythonユーティリティ:ビデオに一括でウォーターマークを追加する

直接に Python の ffmpeg ライブラリを使用する方が良いです。言わずもがな、それを試してみましょう。

紹介#

FFmpeg は強力な音声・動画処理プログラムであり、多くの音声・動画ソフトウェアの基盤でもあります。実際、FFmpeg は業界の音声・動画処理の標準となっています。しかし、FFmpeg のコマンドライン使用には学習コストがかかりますが、ffmpeg-python ライブラリはこの問題をうまく解決しています。

pip を使って簡単にインストールした後、Python のコードで ffmpeg を使用できます。

pip3 install ffmpeg-python

動画情報の取得#

path = 'main.mp4'
probe = ffmpeg.probe(path)
video_stream = next((stream for stream in probe['streams'] if stream['codec_type'] == 'video'), None)
width = int(video_stream['width'])
height = int(video_stream['height'])
print(width, height)

stream を使用して、動画の基本情報(サイズ、長さ、フレームレートなど)を取得できます。

ミラーリング処理#

# 左右ミラーリング
ffmpeg.input(path).hflip().output('output.mp4').run()

# 上下ミラーリング
ffmpeg.input(path).vflip().output('output.mp4').run()

これは、英語の単語「horizontal(水平)」と「vertical(垂直)」の頭文字を簡単に理解できるものです。

ウォーターマークの追加#

main = ffmpeg.input(path)
logo = ffmpeg.input('logo.png')
ffmpeg.filter([main, logo], 'overlay', 0, 500).output('out.mp4').run()

このコマンドは、logo ウォーターマーク画像を main ビデオの上に配置し、座標は(0,500)です。ビデオの左上を原点(0,0)とし、右方向と下方向がそれぞれ x 軸と y 軸を表します。

もちろん、logo を十分に大きくし、ビデオよりも大きくし、両者の位置を入れ替えると、ビデオを logo の上に置くことになります。実際には、ビデオに背景画像を追加したことになります。

ffmpeg.filter([logo, main], 'overlay', 0, 500).output('out.mp4').run()

動画のトリミング#

ffmpeg.input(path).trim(start_frame=10,end_frame=20).output('out3.mp4').run()

このコマンドは、start_frame と end_frame がそれぞれ開始フレームと終了フレームを表します。

動画の結合#

base = ffmpeg.input(path)
ffmpeg.concat(
    base.trim(start_frame=10, end_frame=20),
    base.trim(start_frame=30, end_frame=40),
    base.trim(start_frame=50, end_frame=60)
).output('out3.mp4').run()

動画の結合には concat 関数を使用します。
まとめ
今日は、皆さんに Python で動画を処理するための素晴らしいライブラリを紹介しました。皆さんの仕事や副業の効率向上に役立てていただければ幸いです。

読み込み中...
文章は、創作者によって署名され、ブロックチェーンに安全に保存されています。