Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
sunlubo authored Jul 9, 2018
1 parent a767025 commit 6c2193f
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]) <input file>")
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)
}
```

0 comments on commit 6c2193f

Please sign in to comment.