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
Shumpei Urabe
February 11, 2020
Technology
6.4k
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
120
AIを活用した放送技術スタートアップの制作現場改革について
yaminoma
0
16k
動画配信技術について
yaminoma
2
1.8k
Multimedia Security Topics
yaminoma
0
2.7k
コロナ禍でのライブストリーミングの変化とテレビ放送の規格について
yaminoma
0
1.2k
今日から分かる Android Audioの全て
yaminoma
0
520
Apple Low-Latency HLSを使った 超低遅延配信について
yaminoma
4
5.4k
今日から分かる AVAudioEngineの全て
yaminoma
4
6.5k
Cross Platform Video Player
yaminoma
0
620
Other Decks in Technology
See All in Technology
Building Audio Plugin Ecosystem for Android in 2026
atsushieno
2
160
Gifu WordPress Meetup #200 に向けて、私たちには何ができるだろうか?/ 2026-11-07 Gifu WordPress Meetup
torounit
0
250
The future of architectural work
ufried
0
200
慶應義塾大学 AI実践05 論文執筆の仕組み化
keio_smilab
PRO
7
3.8k
Kiro 入門 (1 つのエージェントを、どの画面からでも) | 目指せ Kiro マスター 01
inariku
PRO
6
1.5k
Ebitengine製iOSアプリ公開ボタンを押す会
eiei114
0
120
AIがGISを代替する日:〜すべてをAIが創る時代に、私たちがFOSS4Gに関わる意味〜
lehupa
1
220
令和の今、あらためて IAM を考える。アクセスキーを「無くせない現実」との向き合い方
kazzpapa3
0
590
セキュリティ専任チームなしで攻撃を先回りするSREが実践したSIEM on Amazon OpenSearch Service
cscengineer
PRO
1
750
業務でAWSをあまり使っていない私が、AWSへのモチベを保つ方法
smt7174
0
190
Bots in Kiro Crew (One point of contact for ongoing work) | Kiro: Zero to Hero 12
inariku
PRO
1
120
Claude Codeで作ったアプリを安全に公開する/Built with Claude Japan 2026
yoshidashingo
0
230
Featured
See All Featured
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
31
10k
We Are The Robots
honzajavorek
0
440
Hiding What from Whom? A Critical Review of the History of Programming languages for Music
tomoyanonymous
3
1.4k
Why Our Code Smells
bkeepers
PRO
340
58k
Are puppies a ranking factor?
jonoalderson
2
4.2k
Data-driven link building: lessons from a $708K investment (BrightonSEO talk)
szymonslowik
1
1.4k
My Coaching Mixtape
mlcsv
0
380
Joys of Absence: A Defence of Solitary Play
codingconduct
1
570
HTML-Aware ERB: The Path to Reactive Rendering @ RubyCon 2026, Rimini, Italy
marcoroth
6
910
Money Talks: Using Revenue to Get Sh*t Done
nikkihalliwell
0
560
From π to Pie charts
rasagy
1
440
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
287
14k
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͕ͬͱΓ্͕͍ͬͯ ͖͍ͨʂ
͓͠·͍