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)

ストリームを使用して、ビデオのサイズ、長さ、フレームレートなどの基本情報を取得できます。

ミラーリング処理#

# 左右ミラーリング
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()

このコマンドは、ロゴのウォーターマーク画像を main ビデオの上に配置し、座標(0,500)に配置することを意味しています。ビデオの左上隅を原点(0,0)と考えると、右方向と下方向にそれぞれ x 軸と y 軸が示されます。

もちろん、ロゴを十分に大きくし、ビデオよりも大きくし、両者の位置を交換すると、ビデオがロゴの上に配置されることになります。実際には、ビデオに背景画像を追加したことになります。

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 でビデオを処理するための素晴らしいライブラリを紹介しました。皆さんの仕事や副業に効率の向上をもたらすことを願っています。

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