Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
PHPとRustを組み合わせて音声ファイルをエンコードする話
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Shumpei Urabe
February 11, 2020
Technology
5.8k
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
PHPとRustを組み合わせて音声ファイルをエンコードする話
PHPerKaigi 2020
https://phperkaigi.jp/2020/
Shumpei Urabe
February 11, 2020
More Decks by Shumpei Urabe
See All by Shumpei Urabe
放送局の“SIer依存”は終わるのか? 〜Claude Code・Devin時代のAIネイティブ放送DX〜
yaminoma
0
91
AIを活用した放送技術スタートアップの制作現場改革について
yaminoma
0
16k
動画配信技術について
yaminoma
2
1.8k
Multimedia Security Topics
yaminoma
0
2.6k
コロナ禍でのライブストリーミングの変化とテレビ放送の規格について
yaminoma
0
1.2k
今日から分かる Android Audioの全て
yaminoma
0
510
Apple Low-Latency HLSを使った 超低遅延配信について
yaminoma
4
5.3k
今日から分かる AVAudioEngineの全て
yaminoma
4
6.4k
Cross Platform Video Player
yaminoma
0
600
Other Decks in Technology
See All in Technology
Sigmaで作る業務アプリ
kazushiro_honma
0
110
20260912_スクフェス三河
kgnkhkr
0
260
深夜のクラウド懺悔室 1:29:300 or 1:0:0
kazzpapa3
1
230
DEFCON_CHV_CTF_Write-up.pdf
bata_24
0
130
あるけみー式LTスライド作成術
alchemy1115
1
160
AIで開発は速くなったのに、なぜ現場は楽にならないのか 〜あなたの組織のボトルネックを突き止めるワークショップ〜
jacopen
1
310
なぜSRE・セキュリティは評価されないのか?守りの組織を事業成長エンジンに変えた実践
cscengineer
PRO
3
2.4k
enechainの内製セルフサービスプラットフォーム
hiyosi
0
150
OpenTelemetry eBPF Instrumentationの舞台裏 / Behind the Scenes of OpenTelemetry eBPF Instrumentation
ymotongpoo
3
960
Redmine 7.0で私が開発した新機能の狙いと背景
vividtone
1
150
GoCon2026 - Open Source, Open World
sanposhiho
2
3.1k
ペアプロの価値はコードを書くことだけじゃない
codmoninc
PRO
0
160
Featured
See All Featured
Become a Pro
speakerdeck
PRO
31
6.2k
Optimising Largest Contentful Paint
csswizardry
37
3.9k
WCS-LA-2024
lcolladotor
0
820
Documentation Writing (for coders)
carmenintech
77
5.5k
Have SEOs Ruined the Internet? - User Awareness of SEO in 2025
akashhashmi
0
490
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
Being A Developer After 40
akosma
91
590k
Future Trends and Review - Lecture 12 - Web Technologies (1019888BNR)
signer
PRO
0
3.7k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
38
3k
A Soul's Torment
seathinner
7
3.6k
Public Speaking Without Barfing On Your Shoes - THAT 2023
reverentgeek
1
560
Avoiding the “Bad Training, Faster” Trap in the Age of AI
tmiket
0
230
Transcript
PHPͱRustΛΈ߹ΘͤͯԻ ϑΝΠϧΛΤϯίʔυ͢Δ PHPerKaigi 2020 LT @yaminoma_tw
Shumpei Urabe (@yaminoma_tw) ͖ͳϑϨʔϜϫʔΫ BEAR.Sunday PHPerݟश͍
PHP FFIͷ͓ͳ͠
FFIͱʁ • PHPҎ֎ͷݴޠͰ࣮͞ΕͨωΠςΟϒίʔυΛݺͼग़͢͜ͱ ͕Ͱ͖ΔΈ • FFIωΠςΟϒ࣮ߦͳͷͰɺύϑΥʔϚϯεͷ্͕ظͰ͖ Δ
PHP FFIͷྺ࢙ • Ҏલ͔ΒPECLͰFFIͷΤΫεςϯγϣϯ͋ͬͨ • ͔͠͠ɺ10Ҏ্ߋ৽͞Ε͍ͯͳ͍ঢ়گͰ์ஔ͞Ε͍ͯͨ
PHP FFIͷྺ࢙ • ͦ͜ͰZendࣾͷDmitry StogovࢯɺlibffiΛ֦ͬͨுΛ։ൃͨ͠ • 20191݄ʹࢍ24ɺର15ͱ͍͏ൺֱతᷮࠩͰPHP 7.4ͷ࠾༻ ͕ܾ·Γɺ͜ͷ֦ு͕ແࣄPHP 7.4ʹऔΓࠐ·Εͨ
• ͦͷ݁ՌɺPECLͷFFIΤΫεςϯγϣϯʹൺΔͱɺΠϯλʔϑΣ ΠεͷΈهड़͢Δ͚ͩͰग़དྷΔΑ͏ʹͳͬͨ
ຊ
PHPͰԻϑΝΠϧΛΤϯίʔυͯ͠ΈΔ • ԻϑΝΠϧͷΤϯίʔυͱ͍͑FFmpeg͕ఆ൪Ͱ͋Δ • FFmpegͷίϚϯυΛexecͰୟ͘ͷ؆୯Ͱ͋Δ͕ɺexecͰ ࣮ߦͨ͘͠ͳ͍ͱ͖͋Δ • PHP୯ମͰԻϑΝΠϧΛΤϯίʔυͰ͖Δͱศརʂ
ͱ͍͏͜ͱͰ࡞Γ·ͨ͠
php-mp3-encoder • https://github.com/yaminoma/php-mp3-encoder • RustͰMP3ͷΤϯίʔμʔͰ͋Δlibmp3lameΛͬͯɺWAVϑΝ Πϧ͔ΒMP3ʹΤϯίʔυ͢Δαϯϓϧ • ͜ͷlibmp3lamebindgenΛͬͯRust͔ΒFFIͰݺͼग़͍ͯ͠Δ
͘͠Έ
wavͷதΛ uint8_t* Ͱ͢ Τϯίʔυͨ͠ϑΝΠϧ໊Λ char* Ͱ͢
PHPͰFFIΛ༗ޮʹͯ͠ΈΔ git clone https://github.com/php/php-src.git cd php-src git checkout PHP-7.4 ./configure
--with-ffi make
PHPͰFFIΛ༗ޮʹͯ͠ΈΔ • σϑΥϧτ ffi.enable=preload ʹͳ͓ͬͯΓɺϓϦϩʔυΛ ߦͬͨؔ·ͨCLIΠϯλʔϑΣΠεͰͷΈFFIػೳΛ༻͢ Δ͜ͱ͕Մೳ • ffi.enable=trueʹ͢ΔͱɺશͯͷFFIͷػೳΛ͏͜ͱ͕ग़དྷΔ
RustͰΤϯίʔυ͢Δ෦Λ࣮͢Δ #[no_mangle] pub extern "C" fn encode(data: *mut u8, length:
u32) -> *const c_char { unsafe { let buf: &mut [u8] = core::slice::from_raw_parts_mut(data, length as usize); let mut ptr = io::Cursor::new(buf); let mut wav_reader = hound::WavReader::new(&mut ptr).unwrap(); let mut data = Vec::new(); for sample in wav_reader.samples() { match sample { Ok(smpl) => data.push(smpl), _ => panic!("failed"), } } let timestamp = Rational64::from_integer(0); let raw_frame = RawFrame { timestamp, data }; let mut encoder = Encoder::new(); let output_file_name = encoder.encode(raw_frame); return CString::new(output_file_name) .expect("failed") .into_raw(); } }
RustͰdylibΛϏϧυͯ͠PHPͰαʔϒ͢Δ $ cargo build --release $ php -d ffi.enable=1 -S
localhost:8000 -t src
μΠφϛοΫϥΠϒϥϦΛಡΈࠐΉ $signature = "const char* encode(uint8_t *data, uint32_t length);"; return
\FFI::cdef( $signature, __DIR__ . "/../target/ release/libmp3encoder.dylib");
freadͰϑΝΠϧΛಡΈऔΔ $file = $_FILES['audio_file']['tmp_name']; $handle = fopen($file, "rb"); $fsize =
filesize($file); $value = fread($handle, $fsize);
FFIͷݺͼग़͠ͱ࣮ߦ $ffi = loadLibrary(); $length = \strlen($value); $buffer = FFI::new(FFI::arrayType($ffi->type('uint8_t'),
[$length])); FFI::memcpy($buffer, $value, $length); $bufferPtr = FFI::cast($ffi->type('uint8_t*'), $buffer); $outputFileName = $ffi->encode($bufferPtr, $length);
݁Ռ • 5ͷۂ͕େମ8ඵ͘Β͍Ͱม͕ऴΘͬͨ • PHPͷFFIͷυΩϡϝϯτ͕ใྔগͳͯ݁͘ߏۤ͠Μͩ • FFI͕༗ޮʹͳ͍ͬͯΕͲͷαʔόͰMP3͕ΤϯίʔυͰ͖ Δʂ
FFIͷকདྷ • ܭࢉྔͷଟ͍ϓϩάϥϜΛFFIͰݺͼग़͢ͱྑ͍͔͠Εͳ͍ • ·ͩ·࣮ͩݧతͳཁૉ͕ଟ͍ͷͰɺϓϩμΫγϣϯϨσΟʔͰ ͳ͍ • ͜Ε͔ΒଞͷݴޠͱΈ߹ΘͤͯPHP͕ͬͱΓ্͕͍ͬͯ ͖͍ͨʂ
͓͠·͍