SlideShare uma empresa Scribd logo
1 de 36
Baixar para ler offline
GIFから12倍速くする
ImageFluxコース
PIXIV SUMMER BOOT CAMP 2022
筑波大学
momeemt / 浅田睦葉
2022.9.28
2
自己紹介
● 筑波大学 情報学群 情報科学類 1年次
● Nim / FFmpeg
● 趣味: インディーズバンドとお笑い
○ 35.7とダウ90000が好きです
● Twitter: @momeemt
momeemt
ImageFluxコース
3
nontanさん usaさん terryさん hayaさん
配信技術部
メンター サブメンター
4
H.264エンコーダを利用して
GIFアニメーションをmp4に変換する
5
● GIFとは複数枚の画像をコマ送りにしてアニメーションにできる
画像ファイルフォーマットの
こと
● Webブラウザでも標準的にサポート
● ImageFluxもサポートしている
GIF?
▲こういうの
6
● H.264が行うフレーム間予測というメチャクチャ賢い圧縮ができない
○ → ファイルサイズが大きくなるので配信コストが高い
● エンコードが遅い
○ どうして...
GIFの問題点
地球も泣いている
● H.264とは動画圧縮規格の1つ
● 「.mp4」「.mov」「.m4v」などおなじみ(?)の拡張子を持つ動画は
H.264コーデックによっ
てエンコード(圧縮)されている
● 軽い!速い!
7
H.264?
8
● FFmpegは映像や音声を処理できる伝統のライブラリ
● 膨大なコーデックやファイル形式、メディアをサポートしている
● コマンドラインツールとしては有名だが
libavcodec等のAPIを直接叩くことも
○ 今回はこっち
FFmpeg
9
● nasm・yasm・libx264・FFmpegをインストール
インストールバトル
10
● ビルド時間がえらいことに(15分半)
長すぎ
11
● H.264コーデックを手に入れる
● エンコードに必要な設定を行う
● 書き出し先である映像ストリームを準備する
● フレームをエンコードしてパケットに格納して書き出す
H.264エンコーダを呼び出す
12
● cgoはCとGoを相互運用する仕組み
○ FFmpegの各種APIはCで記述されているのでこれを使って呼び出す
○ GoのFFmpegラッパーではうまくいかなかったので途中で使うのをやめた
cgo
13
あとは根気
● これはもうC言語
14
● VLCやブラウザで視聴可能なmp4ファイルを出力できた
● QuickTimeではブラックスクリーンになり見れない
○ 気難しい
できた
mp4になった地球
困る
15
● ImageFlux上でGIFをmp4に変換する
● URLで f=mp4 というパラメータを与えるとmp4が返ってくるようにしたい
ImageFluxでmp4変換をサポートする
16
● これまでは書き出すためにavio_open 関数を呼んでいた
○ URLを受け取って書き出し先を確立する
● しかし、ImageFluxではHTTPのレスポンスとして書き出す必要があるので、io.Writer に書
き出さなければならない
● そこでカスタムなAVIOContext を作成して io.Writer を繋ぎ合わせる
I/Oを繋ぎ合わせる
AVIOContext *allocIOContext(void *bridge) {
int bufferSize = 4096;
uint8_t *buffer = (uint8_t *)av_malloc(bufferSize);
return avio_alloc_context(
buffer, bufferSize, 1,
bridge, 0, golibmp4BridgeWriterWrite, golibmp4BridgeSeek
);
}
void freeIOContextBuffer(AVIOContext *ctx) {
av_free(ctx->buffer);
}
I/Oを繋ぎ合わせる
C側から関数ポインタを渡す
17
● AVIOContextは書き込み関数だけでなくシーク関数を呼んでいる
18
I/Oを繋ぎ合わせる
f544a9e55d239e42d74c9a16971e4b234da64441cd7bd
9e17c199b60b09fbe3741fda6110ee2fd16514d88f280b
62c1989204ba2db16d36d650419aeb04e4b5bc762b81
87f91c5d7845…
基本的には最初から順番に書き込んでいくが...
● AVIOContextは書き込み関数だけでなくシーク関数を呼んでいる
19
I/Oを繋ぎ合わせる
f544a9e55d239e42d74c9a16971e4b234da64441cd7bd
9e17c199b60b09fbe3741cd50…fda6110ee2fd16514d8
8f280b62c1989204ba2db16d36d650419aeb04e4b5bc7
62b8187f91c5d7845
ファイルの途中に巻き戻って書き出すこともある
● ImageFluxは io.Writer を受け取る関数が処理の基本単位になっている
○ io.Writer はシークできることが保証されていないのでシークできない
● エンコードする時にシーク可能なバッファに書き込んで、まとめて書き出す
○ メンターのnontanさんが seekbuf を実装してくださった
実装しておきました
20
I/Oを繋ぎ合わせる
ありがとうございます🙏
21
動いた
~/c!/f=mp4/testdata/images/rotating_earth.gif
22
● 利用しやすいOpenH264に差し替える
● libopenh264をビルドしてインストール(pkg-configファイルの生成)
● libopenh264が生成した共有ライブラリを削除して
Ciscoがビルドして頒布している共有ラ
イブラリに差し替える
● OpenH264を有効にしてFFmpegをビルド
OpenH264への差し替え
23
● GIFよりどれくらい速くなったのか
● GIFエンコードとmp4エンコードのベンチマークを書いて計測する
計測!
1回の実行に561ミリ秒
GIFエンコード
● GIFよりどれくらい速くなったのか
● GIFエンコードとmp4エンコードのベンチマークを書いて計測する
24
もう速い
mp4エンコード
1回の実行に69ミリ秒
6.3倍
25
お得
ファイルサイズも半分少しになっている
26
● しかしメモリ確保を43倍やっているのでもう少し減らしたい
富豪すぎた
mp4エンコード
1回の実行に176万回もメモリを確保している
27
● NRGBAをYUV420Pに変換する時にメモリアロケーション回数が跳ね上がっていた
エンコード最適化
28
● color.NRGBA 型が color.Color 型にBoxingされる際にメモリアロケーションがされまくっ
ていた
○ → image.NRGBA 型にアサーションすることでアロケーションを回避
エンコード最適化
● 元のGIFエンコードよりも12.2倍 高速になった
● また、メモリ確保回数はGIFエンコードと比べて0.013% になった
29
結果
1回の実行に46ミリ秒
爆速
もっと!
30
※ 最終日はずっとチューニングしていた
● 色空間の変換で毎フレーム確保されていたバッファを使い回す
● 元のGIFエンコードよりも14.8倍 高速になった
31
NRGBA32→YUV420P変換
1回の実行に38ミリ秒
● 色空間の変換で23ms、フレームの書き込みで15msかかっている
● どちらかを改善したい
32
速度的なボトルネック
変換部分のありなしで 2桁も変わる
● 自前の色空間変換を捨ててlibswscale を使ってみる
○ SWS_FAST_BILINEAR フラグで変換するとかなり速かった(約
17ms)
● 元のGIFエンコードよりも19.1倍 高速になった
33
libswscaleを使う
1回の実行に29ミリ秒
● なったが、かなり変換時間にはブレがある
○ 自前の場合は安定して37ms〜42msかかっていた
○ swscaleでは27〜32msかかることは多いが、たまに60msになったりする
34
libswscaleを使う
1回の実行に29ミリ秒
35
感想
● 複数の環境で問題なく動作すること、いずれ本番環境に乗せることを見据えて品質を保
つことなどを強く意識させられた
8日間でした
● FFmpegを延々と触っていられて楽しかったです
○ 実装のためにドキュメントを普段よりじっくりと読めたので解像度がなんとなく上がった
気がします
● シェルが書けないとこんなに辛いんだなと思いました
○ 8日間で一番苦労したのはシェルスクリプトでした
36
8日間ありがとうございました!!!
最後に
メンターのnontanさん、人事のkamikoさん、配信技術部のみなさま、
お世話になりました、ありがとうございました!

Mais conteúdo relacionado

Último

sm支付宝号购买【☆出售网址:fk578。com☆】【☆购买网址:fk578。com☆】抖音小号自动发号
sm支付宝号购买【☆出售网址:fk578。com☆】【☆购买网址:fk578。com☆】抖音小号自动发号sm支付宝号购买【☆出售网址:fk578。com☆】【☆购买网址:fk578。com☆】抖音小号自动发号
sm支付宝号购买【☆出售网址:fk578。com☆】【☆购买网址:fk578。com☆】抖音小号自动发号sundstromkichline246
 
QQ实名号自助购买【☆出售网址:fk578。com☆】【☆购买网址:fk578。com☆】实名微博号购买商城
QQ实名号自助购买【☆出售网址:fk578。com☆】【☆购买网址:fk578。com☆】实名微博号购买商城QQ实名号自助购买【☆出售网址:fk578。com☆】【☆购买网址:fk578。com☆】实名微博号购买商城
QQ实名号自助购买【☆出售网址:fk578。com☆】【☆购买网址:fk578。com☆】实名微博号购买商城sundstromkichline246
 
失忆方法迷幻药【购买网址: GHB1.com】 拍肩粉货到付款购买【商城网址: 91miwan.com☆】
失忆方法迷幻药【购买网址:  GHB1.com】 拍肩粉货到付款购买【商城网址:  91miwan.com☆】失忆方法迷幻药【购买网址:  GHB1.com】 拍肩粉货到付款购买【商城网址:  91miwan.com☆】
失忆方法迷幻药【购买网址: GHB1.com】 拍肩粉货到付款购买【商城网址: 91miwan.com☆】cromandougharty
 
购买官网听话水【购买网址: GHB1.com】 迷幻药货到付款【商城网址: 91miwan.com☆】
购买官网听话水【购买网址:  GHB1.com】 迷幻药货到付款【商城网址:  91miwan.com☆】购买官网听话水【购买网址:  GHB1.com】 迷幻药货到付款【商城网址:  91miwan.com☆】
购买官网听话水【购买网址: GHB1.com】 迷幻药货到付款【商城网址: 91miwan.com☆】cromandougharty
 
微信白号怎么买【☆出售网址:fk578。com☆】【☆购买网址:fk578。com☆】soul账号交易平台
微信白号怎么买【☆出售网址:fk578。com☆】【☆购买网址:fk578。com☆】soul账号交易平台微信白号怎么买【☆出售网址:fk578。com☆】【☆购买网址:fk578。com☆】soul账号交易平台
微信白号怎么买【☆出售网址:fk578。com☆】【☆购买网址:fk578。com☆】soul账号交易平台sundstromkichline246
 
境外手机卡商城【☆购买网站:fk578.com】【☆购买网址:fk578.com☆】☆☆快手账号交易平台
境外手机卡商城【☆购买网站:fk578.com】【☆购买网址:fk578.com☆】☆☆快手账号交易平台境外手机卡商城【☆购买网站:fk578.com】【☆购买网址:fk578.com☆】☆☆快手账号交易平台
境外手机卡商城【☆购买网站:fk578.com】【☆购买网址:fk578.com☆】☆☆快手账号交易平台sundstromkichline246
 
微信实名号哪里买【☆出售网址:fk578。com☆】【☆购买网址:fk578。com☆】soul实名账号批发
微信实名号哪里买【☆出售网址:fk578。com☆】【☆购买网址:fk578。com☆】soul实名账号批发微信实名号哪里买【☆出售网址:fk578。com☆】【☆购买网址:fk578。com☆】soul实名账号批发
微信实名号哪里买【☆出售网址:fk578。com☆】【☆购买网址:fk578。com☆】soul实名账号批发sundstromkichline246
 

Último (7)

sm支付宝号购买【☆出售网址:fk578。com☆】【☆购买网址:fk578。com☆】抖音小号自动发号
sm支付宝号购买【☆出售网址:fk578。com☆】【☆购买网址:fk578。com☆】抖音小号自动发号sm支付宝号购买【☆出售网址:fk578。com☆】【☆购买网址:fk578。com☆】抖音小号自动发号
sm支付宝号购买【☆出售网址:fk578。com☆】【☆购买网址:fk578。com☆】抖音小号自动发号
 
QQ实名号自助购买【☆出售网址:fk578。com☆】【☆购买网址:fk578。com☆】实名微博号购买商城
QQ实名号自助购买【☆出售网址:fk578。com☆】【☆购买网址:fk578。com☆】实名微博号购买商城QQ实名号自助购买【☆出售网址:fk578。com☆】【☆购买网址:fk578。com☆】实名微博号购买商城
QQ实名号自助购买【☆出售网址:fk578。com☆】【☆购买网址:fk578。com☆】实名微博号购买商城
 
失忆方法迷幻药【购买网址: GHB1.com】 拍肩粉货到付款购买【商城网址: 91miwan.com☆】
失忆方法迷幻药【购买网址:  GHB1.com】 拍肩粉货到付款购买【商城网址:  91miwan.com☆】失忆方法迷幻药【购买网址:  GHB1.com】 拍肩粉货到付款购买【商城网址:  91miwan.com☆】
失忆方法迷幻药【购买网址: GHB1.com】 拍肩粉货到付款购买【商城网址: 91miwan.com☆】
 
购买官网听话水【购买网址: GHB1.com】 迷幻药货到付款【商城网址: 91miwan.com☆】
购买官网听话水【购买网址:  GHB1.com】 迷幻药货到付款【商城网址:  91miwan.com☆】购买官网听话水【购买网址:  GHB1.com】 迷幻药货到付款【商城网址:  91miwan.com☆】
购买官网听话水【购买网址: GHB1.com】 迷幻药货到付款【商城网址: 91miwan.com☆】
 
微信白号怎么买【☆出售网址:fk578。com☆】【☆购买网址:fk578。com☆】soul账号交易平台
微信白号怎么买【☆出售网址:fk578。com☆】【☆购买网址:fk578。com☆】soul账号交易平台微信白号怎么买【☆出售网址:fk578。com☆】【☆购买网址:fk578。com☆】soul账号交易平台
微信白号怎么买【☆出售网址:fk578。com☆】【☆购买网址:fk578。com☆】soul账号交易平台
 
境外手机卡商城【☆购买网站:fk578.com】【☆购买网址:fk578.com☆】☆☆快手账号交易平台
境外手机卡商城【☆购买网站:fk578.com】【☆购买网址:fk578.com☆】☆☆快手账号交易平台境外手机卡商城【☆购买网站:fk578.com】【☆购买网址:fk578.com☆】☆☆快手账号交易平台
境外手机卡商城【☆购买网站:fk578.com】【☆购买网址:fk578.com☆】☆☆快手账号交易平台
 
微信实名号哪里买【☆出售网址:fk578。com☆】【☆购买网址:fk578。com☆】soul实名账号批发
微信实名号哪里买【☆出售网址:fk578。com☆】【☆购买网址:fk578。com☆】soul实名账号批发微信实名号哪里买【☆出售网址:fk578。com☆】【☆购买网址:fk578。com☆】soul实名账号批发
微信实名号哪里买【☆出售网址:fk578。com☆】【☆购买网址:fk578。com☆】soul实名账号批发
 

Destaque

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 

Destaque (20)

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 

PIXIV SUMMER BOOT CAMP 2022 成果発表