From 6c2193f7db95429357678111e0b47ad25bca2e15 Mon Sep 17 00:00:00 2001 From: sun_ Date: Mon, 9 Jul 2018 23:16:02 +0800 Subject: [PATCH] Update README.md --- README.md | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/README.md b/README.md index bade608..f6f988c 100644 --- a/README.md +++ b/README.md @@ -19,3 +19,86 @@ dependencies: [ .package(url: "https://github.com/sunlubo/SwiftFFmpeg.git", from: "1.0.0") ] ``` + +## Usage + +```swift +import Foundation +import SwiftFFmpeg + +func main() throws { + if CommandLine.argc < 2 { + print("Usage: \(CommandLine.arguments[0]) ") + return + } + let input = CommandLine.arguments[1] + + let fmtCtx = AVFormatContext() + try fmtCtx.openInput(input) + try fmtCtx.findStreamInfo() + + let stream = fmtCtx.videoStream! + let codecpar = stream.codecpar + guard let codec = AVCodec.findDecoderById(codecpar.codecId) else { + print("Codec not found") + return + } + guard let codecCtx = AVCodecContext(codec: codec) else { + print("Could not allocate video codec context.") + return + } + try codecCtx.setParameters(stream.codecpar) + try codecCtx.openCodec() + + let pkt = AVPacket()! + let frame = AVFrame()! + + var num = 50 + while let _ = try? fmtCtx.readFrame(into: pkt) { + if pkt.streamIndex != stream.index { + pkt.unref() + continue + } + + do { + try codecCtx.sendPacket(pkt) + } catch { + print("Error while sending a packet to the decoder: \(error)") + return + } + + while true { + do { + try codecCtx.receiveFrame(frame) + } catch let err as AVError where err == .EAGAIN || err == .EOF { + break + } catch { + print("Error while receiving a frame from the decoder: \(error)") + return + } + + let str = String(format: "Frame %2d (type=%@, size=%5d bytes) pts %6lld key_frame %d [DTS %2d]", + codecCtx.frameNumber, + frame.pictType.description, + frame.pktSize, + frame.pts, + frame.isKeyFrame, + frame.codedPictureNumber) + print(str) + + frame.unref() + } + + num -= 1 + if num <= 0 { break } + + pkt.unref() + } +} + +do { + try main() +} catch { + print(error) +} +```