Upgrade to Pro — share decks privately, control downloads, hide ads and more …

iOSでmp4をデコードして自分だけの動画プレーヤーを作ろう!/orecon

noppefoxwolf
September 12, 2018

 iOSでmp4をデコードして自分だけの動画プレーヤーを作ろう!/orecon

noppefoxwolf

September 12, 2018
Tweet

More Decks by noppefoxwolf

Other Decks in Programming

Transcript

  1. "71MBZFS$POUSPMMFS ࢖͍ํ let vc = AVPlayerViewController() vc.player = AVPlayer(url: url)

    present(vc, animated: true, completion: nil) PSFDPO@JPTD 
  2. "71MBZFS-BZFS let player = AVPlayer(url: url) player.play() let layer =

    AVPlayerLayer(player: player) layer.frame = frame view.layer.addSublayer(layer) PSFDPO@JPTD 
  3. "7"TTFU3FBEFS let asset = AVURLAsset(url: url) let video = asset.tracks(withMediaType:

    .video).first! let reader = try! AVAssetReader(asset: asset) let output = AVAssetReaderTrackOutput(track: video) if reader.canAdd(output) { reader.add(output) } output.copyNextSampleBuffer() output.copyNextSampleBuffer() output.copyNextSampleBuffer() PSFDPO@JPTD 
  4. "7"TTFU3FBEFS ඳըઌ͸ࣗ༝ w 6**NBHF7JFX let image = CIImage(cvPixelBuffer: pb) imageView.image

    = UIImage(ciimage: image) w "74BNQMF#V⒎FS%JTQMBZ-BZFS let sb = output.copyNextSampleBuffer() displayLayer.enqueue(sb) PSFDPO@JPTD 
  5. "7"TTFU3FBEFS w .FUBM CVMetalTextureCacheCreateTextureFromImage(kCFAllocatorDefault, textureCache, imageBuffer, nil, pixelFormat, width, height,

    0, &imageTexture) // ~ লུ commandBuffer.present(drawable) commandBuffer.commit() PSFDPO@JPTD 
  6. TXJGUͰNQΛCPY͝ͱʹύʔε͢Δ *OQVU4USFBNͰ  CPY4J[Fͷॱ൪ͰಡΈଓ͚Ε͹ 0, ಛఆͷCPYͰ͸ɺࣗ਎ͷCPYʹରͯ͠࠶౓ݕࡧΛߦ͏ let stream = InputStream(url:

    url) stream.open() while stream.hasBytesAvailable { let size = stream.readUint32() let type = stream.readAscii(4) let data = stream.read(size - 8) } IUUQTHJUIVCDPNOPQQFGPYXPMG.1#PY%VNQFSTXJGU PSFDPO@JPTD 
  7. GUZQ CZUF͝ͱʹαϙʔτ͍ͯ͠Δϒϥϯυ͕ฒͿ ઌ಄͸Major Brandɺͦͷ͋ͱ͸Compatible Brand Major Brand: mp42 Compatible Brand:

    isom Compatible Brand: mp42 .BKPSͱ$PNQBUJCMFͷؒʹ͸CZUFۭ͘ IUUQXXXGUZQTDPN PSFDPO@JPTD 
  8. 75%FDPNQSFTT4FTTJPOͷॳظԽ let desc: CMVideoFormatDescription = ~~ let callback = {

    (imageBuffer: CVImageBuffer?) in } let attrib = [.pixelFormat : .gbra] let session = VTDecompressSession(desc, params, attrib, callback) PSFDPO@JPTD 
  9. $.7JEFP'PSNBU%FTDSJQUJPO let pps: [UInt8] = ~~ let sps: [UInt8] =

    ~~ let desc = CMVideoFormatDescriptionCreateFromH264ParameterSets([pps, pps]) PSFDPO@JPTD