mirror of
https://github.com/pion/webrtc.git
synced 2025-12-24 11:51:03 +08:00
Add support for AV1 and VP9 to play from disk
This commit is contained in:
@@ -13,10 +13,26 @@ git clone https://github.com/pion/webrtc.git
|
||||
cd webrtc/examples/play-from-disk-renegotiation
|
||||
```
|
||||
|
||||
### Create IVF named `output.ivf` that contains a VP8 track
|
||||
### Create IVF named `output.ivf` that contains a VP8, VP9 or AV1 track
|
||||
|
||||
To encode video to VP8:
|
||||
```
|
||||
ffmpeg -i $INPUT_FILE -g 30 -b:v 2M output.ivf
|
||||
ffmpeg -i $INPUT_FILE -c:v libvpx -g 30 -b:v 2M output.ivf
|
||||
```
|
||||
|
||||
alternatively, to encode video to AV1 (Note: AV1 is CPU intensive, you may need to adjust `-cpu-used`):
|
||||
```
|
||||
ffmpeg -i $INPUT_FILE -c:v libaom-av1 -cpu-used 8 -g 30 -b:v 2M output.ivf
|
||||
```
|
||||
|
||||
Or to encode video to VP9:
|
||||
```
|
||||
ffmpeg -i $INPUT_FILE -c:v libvpx-vp9 -cpu-used 4 -g 30 -b:v 2M output.ivf
|
||||
```
|
||||
|
||||
If you have a VP8, VP9 or AV1 file in a different container you can use `ffmpeg` to mux it into IVF:
|
||||
```
|
||||
ffmpeg -i $INPUT_FILE -c:v copy -an output.ivf
|
||||
```
|
||||
|
||||
**Note**: In the `ffmpeg` command, the argument `-b:v 2M` specifies the video bitrate to be 2 megabits per second. We provide this default value to produce decent video quality, but if you experience problems with this configuration (such as dropped frames etc.), you can decrease this. See the [ffmpeg documentation](https://ffmpeg.org/ffmpeg.html#Options) for more information on the format of the value.
|
||||
|
||||
@@ -71,9 +71,32 @@ func createPeerConnection(res http.ResponseWriter, req *http.Request) {
|
||||
}
|
||||
|
||||
// Add a single video track.
|
||||
func addVideo(res http.ResponseWriter, req *http.Request) {
|
||||
func addVideo(res http.ResponseWriter, req *http.Request) { //nolint:cyclop
|
||||
// Open a IVF file and start reading using our IVFReader
|
||||
file, err := os.Open("output.ivf")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
ivf, header, err := ivfreader.NewWith(file)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
var mimeType string
|
||||
switch header.FourCC {
|
||||
case "VP80":
|
||||
mimeType = webrtc.MimeTypeVP8
|
||||
case "VP90":
|
||||
mimeType = webrtc.MimeTypeVP9
|
||||
case "AV01":
|
||||
mimeType = webrtc.MimeTypeAV1
|
||||
default:
|
||||
panic(fmt.Sprintf("unsupported codec: %s", header.FourCC))
|
||||
}
|
||||
|
||||
videoTrack, err := webrtc.NewTrackLocalStaticSample(
|
||||
webrtc.RTPCodecCapability{MimeType: webrtc.MimeTypeVP8},
|
||||
webrtc.RTPCodecCapability{MimeType: mimeType},
|
||||
fmt.Sprintf("video-%d", randutil.NewMathRandomGenerator().Uint32()),
|
||||
fmt.Sprintf("video-%d", randutil.NewMathRandomGenerator().Uint32()),
|
||||
)
|
||||
@@ -97,9 +120,9 @@ func addVideo(res http.ResponseWriter, req *http.Request) {
|
||||
}
|
||||
}()
|
||||
|
||||
go writeVideoToTrack(videoTrack)
|
||||
doSignaling(res, req)
|
||||
fmt.Println("Video track has been added")
|
||||
go writeVideoToTrack(ivf, header, videoTrack)
|
||||
}
|
||||
|
||||
// Remove a single sender.
|
||||
@@ -163,18 +186,9 @@ func main() {
|
||||
|
||||
// Read a video file from disk and write it to a webrtc.Track
|
||||
// When the video has been completely read this exits without error.
|
||||
func writeVideoToTrack(track *webrtc.TrackLocalStaticSample) {
|
||||
// Open a IVF file and start reading using our IVFReader
|
||||
file, err := os.Open("output.ivf")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
ivf, header, err := ivfreader.NewWith(file)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
func writeVideoToTrack(
|
||||
ivf *ivfreader.IVFReader, header *ivfreader.IVFFileHeader, track *webrtc.TrackLocalStaticSample,
|
||||
) {
|
||||
// Send our video file frame at a time. Pace our sending so we send it at the same speed it should be played back as.
|
||||
// This isn't required since the video is timestamped, but we will such much higher loss if we send all at once.
|
||||
//
|
||||
|
||||
Reference in New Issue
Block a user