在手機web中播放視頻(使用js,不使用video標籤,支持直播)

主要原理是使用 jsmpeg(Github連接)git

jsmpeg是js中解析mpeg視頻,並把內容畫在畫布上。

這篇文章是記錄jsmpeg怎麼用的。github

目前發現jsmpeg的不足

  1. 沒法播放聲音,只能播放視頻。(2018年更新,官方已解決該問題)
  2. 在蘋果和性能低的安卓中卡頓嚴重(iPhone7有略微卡頓,部分2016年安卓旗艦機不卡,如今終於相信有安卓能秒蘋果了)
  3. 目前(2018年)版本使用ts格式的視頻,相同質量的視頻大小差很少是mp4的兩倍,太佔流量了
  4. 缺乏許多事件

解決不足

  1. audio播放
  2. 沒法解決,我原本想本身開啓webgl,結果看到jsmpeg就是用的webgl,就是說他本身已經解決過了的。隨着你們手機的升級,已經不卡了
  3. 也沒法解決,只能在體積和可操做性上作取捨了
  4. 我本身增長了幾個事件:https://gitee.com/haua/jsmpeg...

視頻源

直播流

jsmpeg支持直播流,可是我還沒研究,因此只是先提一下,先不深刻。web

視頻文件

jsmpeg只支持mpeg格式的視頻(目前2018版本支持ts視頻格式,彷佛是不支持mpeg了?),jsmpeg官方建議用ffmpeg來轉格式。ffmpeg下載地址canvas

  1. 從把視頻轉成mpg格式(轉出的視頻無音頻,且應該是支持從許多種格式轉過來的,我暫時只試過從mp4轉)
    ./ffmpeg -i video.mp4 -f mpeg1video -vf "crop=iw-mod(iw,2):ih-mod(ih,2)" -b 0 video.mpg
  2. 從視頻中提取音頻(上面說過jsmpeg不能播聲音,因此聲音要單獨弄出來)
    ./ffmpeg -i video.mp4 -f mp3 -vn video.mp3

ffmpeg用法記錄
Print help / information / capabilities:
-L show license
-h topic show help
-? topic show help
-help topic show help
--help topic show help
-version show version
-buildconf show build configuration
-formats show available formats
-devices show available devices
-codecs show available codecs
-decoders show available decoders
-encoders show available encoders
-bsfs show available bit stream filters
-protocols show available protocols
-filters show available filters
-pix_fmts show available pixel formats
-layouts show standard channel layouts
-sample_fmts show available audio sample formats
-colors show available color names
-sources device list sources of the input device
-sinks device list sinks of the output device
-hwaccels show available HW acceleration methodside

Global options (affect whole program instead of just one file:
-loglevel loglevel set logging level
-v loglevel set logging level
-report generate a report
-max_alloc bytes set maximum size of a single allocated block
-y overwrite output files
-n never overwrite output files
-ignore_unknown Ignore unknown stream types
-stats print progress report during encoding
-max_error_rate ratio of errors (0.0: no errors, 1.0: 100% error maximum error rate
-bits_per_raw_sample number set the number of bits per raw sample
-vol volume change audio volume (256=normal)性能

Per-file main options:
-f fmt force format
-c codec codec name
-codec codec codec name
-pre preset preset name
-map_metadata outfile[,metadata]:infile[,metadata] set metadata information of outfile from infile
-t duration record or transcode "duration" seconds of audio/video
-to time_stop record or transcode stop time
-fs limit_size set the limit file size in bytes
-ss time_off set the start time offset
-sseof time_off set the start time offset relative to EOF
-seek_timestamp enable/disable seeking by timestamp with -ss
-timestamp time set the recording timestamp ('now' to set the current time)
-metadata string=string add metadata
-program title=string:st=number... add program with specified streams
-target type specify target file type ("vcd", "svcd", "dvd", "dv" or "dv50" with optional prefixes "pal-", "ntsc-" or "film-")
-apad audio pad
-frames number set the number of frames to output
-filter filter_graph set stream filtergraph
-filter_script filename read stream filtergraph description from a file
-reinit_filter reinit filtergraph on input parameter changes
-discard discard
-disposition dispositionwebgl

Video options:
-vframes number set the number of video frames to output
-r rate set frame rate (Hz value, fraction or abbreviation)
-s size set frame size (WxH or abbreviation)
-aspect aspect set aspect ratio (4:3, 16:9 or 1.3333, 1.7777)
-bits_per_raw_sample number set the number of bits per raw sample
-vn disable video
-vcodec codec force video codec ('copy' to copy stream)
-timecode hh:mm:ss[:;.]ff set initial TimeCode value.
-pass n select the pass number (1 to 3)
-vf filter_graph set video filters
-ab bitrate audio bitrate (please use -b:a)
-b bitrate video bitrate (please use -b:v)
-dn disable dataui

Audio options:
-aframes number set the number of audio frames to output
-aq quality set audio quality (codec-specific)
-ar rate set audio sampling rate (in Hz)
-ac channels set number of audio channels
-an disable audio
-acodec codec force audio codec ('copy' to copy stream)
-vol volume change audio volume (256=normal)
-af filter_graph set audio filterscode

Subtitle options:
-s size set frame size (WxH or abbreviation)
-sn disable subtitle
-scodec codec force subtitle codec ('copy' to copy stream)
-stag fourcc/tag force subtitle tag/fourcc
-fix_sub_duration fix subtitles duration
-canvas_size size set canvas size (WxH or abbreviation)
-spre preset set the subtitle options to the indicated presetorm

相關文章
相關標籤/搜索