音影片技術(3)- 入門DEMO

  • 2020 年 3 月 27 日
  • 筆記

斷斷續續花了一周的業餘時間,完成了4個iOS端 ffmpeg demo的實現

  1. 做個簡單的入口

image

  1. 接入ffmpeg編譯好的庫和頭文件,列印configuration

image

  1. 推流的實現 推流的實現,需要先搭建一個nginx + rtmp伺服器,也不複雜,網上有很多詳細的教程 推流後,本地可以用ffplay來查看推流的影片,也可以寫個簡單的h5頁面查看 推流最末尾會報錯,錯誤如下,google/baidu 找遍了所有的帖子,沒找到原因 // av_write_trailer(ofmt_ctx); 只能確定是這句程式碼有問題,應該是寫流結尾出bug了, 可能是採用了老介面的問題 Error muxing packet [flv @ 0x12102c000] Failed to update header with correct duration. [flv @ 0x12102c000] Failed to update header with correct filesize. Error occurred.

也可以直接用ffmpeg命令推流

ffmpeg -re -i "/home/users/test.mp4" -vcodec libx264 -vprofile baseline -acodec aac -ar 44100 -strict -2 -ac 1 -f flv -s 1280x720 -q 10 rtmp://xxxxx:1935/hls/test2
// 直接用ffplay接受流媒體數據  // ffplay -i rtmp://180.76.164.113:1935/hls/test2    // h5實現程式碼, 引用了hls庫:   <!DOCTYPE html>  <html>  <head>      <title>rtmp</title>      <meta charset="utf-8">      <script src="https://cdn.jsdelivr.net/hls.js/latest/hls.min.js"></script>  </head>  <body>      非的發達      <video      autoplay="true"      muted="muted"      id="video"></video>      <script>          if (Hls.isSupported()) {              var video = document.getElementById('video');              var hls = new Hls();              hls.loadSource('http://xxxxx/hls/test2.m3u8');              hls.attachMedia(video);              hls.on(Hls.Events.MANIFEST_PARSED, function(){                  video.play();              })          }      </script>  </body>  </html>
  1. iOS原生播放器的實現

image.png iOS對音影片的支援非常好,寫個播放的demo,總共不到80行程式碼

#import "PlayViewController.h"  #import <MediaPlayer/MediaPlayer.h>  @interface PlayViewController ()  @property MPMoviePlayerController *moviePlayer;  @end    @implementation PlayViewController    - (void)viewDidLoad {      [super viewDidLoad];      [self.moviePlayer play];  }    -(void)dealloc{      [[NSNotificationCenter defaultCenter] removeObserver:self];  }    -(MPMoviePlayerController *)moviePlayer{      if(!_moviePlayer){          NSString *urlStr = [[[NSBundle mainBundle]resourcePath] stringByAppendingPathComponent:@"resource.bundle/war3end.mp4"];          NSURL *url = [NSURL fileURLWithPath:urlStr];          _moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];          _moviePlayer.view.frame = self.view.bounds;          _moviePlayer.view.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;          [self.view addSubview:_moviePlayer.view];      }      return _moviePlayer;  }    -(void)addNotification{      NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];      [notificationCenter addObserver:self selector:@selector(mediaPlayerPlaybackStateChange:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:self.moviePlayer];      [notificationCenter addObserver:self selector:@selector(mediaPlayerPlaybackfinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayer];  }    -(void)mediaPlayerPlaybackStateChange:(NSNotification *)notification{      switch (self.moviePlayer.playbackState) {          case MPMoviePlaybackStatePlaying:              NSLog(@"正在播放...");              break;          case MPMoviePlaybackStatePaused:              NSLog(@"暫停播放");              break;          case MPMoviePlaybackStateStopped:              NSLog(@"停止播放");              break;          default:              break;      }  }    -(void)mediaPlayerPlaybackfinished:(NSNotification *)notification{      NSLog(@"播放完成.%li", self.moviePlayer.playbackState);  }    @end