Created
May 31, 2018 17:46
-
-
Save LeifAndersen/4fe6316aa7a35a243a161c3d9c1877c7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; Helper function to turn a file into a string bundle. | |
(define (file->stream-bundle file) | |
(define avformat (avformat-open-input file #f #f)) | |
(avformat-context->stream-bundle avformat file)) | |
;; General purpose function to create a stream-bundle from | |
;; an ffmpeg level avformat-context. This pulls out the various streams | |
;; and information about them (such as their codecs), and prepares them for | |
;; demux% or some other processor. | |
;; avformat-context-pointer? (or/c path? path-string? #f) -> stream-bundle? | |
(define (avformat-context->stream-bundle avformat file) | |
(avformat-find-stream-info avformat #f) | |
(define raw-strs (avformat-context-streams avformat)) | |
(define stream-table (make-hash)) | |
(define streams | |
(for/vector ([i raw-strs] | |
[index (in-naturals)]) | |
(define codec-parameters (avstream-codecpar i)) | |
(define codec-name (avcodec-parameters-codec-type codec-parameters)) | |
(define codec-id (avcodec-parameters-codec-id codec-parameters)) | |
(define codec (avcodec-find-decoder codec-id)) | |
(define codec-ctx (avcodec-parameters-to-context codec codec-parameters)) | |
(when (eq? codec-name 'video) | |
(set-avcodec-context-time-base! codec-ctx (avstream-time-base i)) | |
(set-avcodec-context-framerate! codec-ctx (av-guess-frame-rate avformat i #f))) | |
(avcodec-open2 codec-ctx codec #f) | |
(define obj (mk-codec-obj #:codec-parameters codec-parameters | |
#:type codec-name | |
#:index index | |
#:stream i | |
#:id codec-id | |
#:codec codec | |
#:codec-context codec-ctx)) | |
(dict-update! stream-table codec-name | |
(λ (rst) (append rst (list obj))) | |
(λ () '())) | |
obj)) | |
(mk-stream-bundle #:raw-streams raw-strs | |
#:avformat-context avformat | |
#:streams streams | |
#:stream-table stream-table | |
#:file file)) | |
(struct stream-bundle (raw-streams | |
streams | |
stream-table | |
avformat-context | |
options-dict | |
start-offset | |
file) | |
#:mutable) | |
(struct codec-obj (codec-parameters | |
type | |
index | |
id | |
codec | |
codec-context | |
stream | |
pts | |
next-pts | |
dts | |
next-dts | |
buffer | |
buffer-context | |
callback-data | |
extra-parameters | |
start-time | |
flags) | |
#:mutable) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment