当前位置:网站首页>The development summary of the function of fast playback of audio and video in any format on the web page.
The development summary of the function of fast playback of audio and video in any format on the web page.
2022-07-25 15:22:00 【Fish and attack the City lion】
The development focus is pc End , Mobile terminal does not consider .
To realize the transmission of audio and video from the user to the server , After uploading, you can drag the progress bar to watch .
h5 Self contained video Tags do not support most audio and video formats , Don't consider .
Consider using streaming media , Turn the source video into a video stream and push it to the web player .
Use nginx+nginx-rtmp-module+ffmpeg Set up a streaming media server , Use rtmp and hls Experiment with two ways of streaming , use vlc Play successfully , But I found it was live , No progress , I'll have a good time , But I also learned about the realization of live broadcast function . This plan doesn't work , Abandon .
hls Streaming media , It's a m3u8 file , Inside is a list of video clip files , a pile ts Small file in format , So I want to use this way to realize .
use ffmpeg For video m3u8 Automatic segmentation , Will generate a m3u8 Files and a bunch of ts file .
command : ffmpeg -i file name -c:v libx264 -c:a aac -hls_time 10 -hls_list_size 0 -strict -2 -f hls output.m3u8
-c:v -c:a: They are the encoding types of video and audio
-hls_time n: Set the length of each piece , The default value is 2. The unit is in seconds
-hls_list_size n: Set the maximum number of entries saved in the playlist , Set to 0 There will be pieces of information saved , The default value is 5
-hls_wrap n: Set how many pieces to start covering , If set to 0 Will not cover , The default value is 0. This option avoids storing too much on disk
slice , And it can limit the number of pieces written to the disk
-hls_start_number n: Set the in the playlist sequence number The value of is number, The default value is 0
First , Generated file , To find a way to play on the web , Tried several players , In the end ckplayer, The official help document contains how to play m3u8 The method of streaming media , The principle is to use flash Play it , One swf player . The player first requests m3u8 file , Calculate the total progress of the video according to the duration in the file list . Request corresponding when dragging progress ts file . A series of requests are sent by the player .
Try to get the request of the player , Use django Create a project , Set up a static folder , Put the generated video file in , Write it on the webpage m3u8 File path , It will play normally , But cannot listen for player requests . So put the video file in the normal folder , Link according to the player's request , Set up a url Corresponding to a method in the background , This method will be used for every request , You can get which file is requested , Which file is requested , I found this file , Open in stream (open(tsfilepath, 'rb')) And back to , Played successfully .
Realize any progress playback , According to the requested document , It is calculated that the file starts from the second of the source file , From here, you can intercept the video clip and return .
The key codes are as follows :
Sir, it becomes a temporary document
os.system("ffmpeg -i %s -ss %d -t %d -c:v libx264 -c:a aac -g 5 -sc_threshold 0 %s" % ( Source file path , Number one ts file * How long is each segment , How long is each segment , Generate ts route )) Set the start_time To play normally , The specific principle is not very clear .
os.system("ffmpeg -i %s/%s -ss %d -t %d -c:v copy -c:a copy -g 5 -sc_threshold 0 %s/%s" % (tsdirpath, tsname, -tsnum * cuttime, cuttime * (tsnum + 1), tsdirpath, tsfile))
os.system("rm -f %s/%s" % (tsdirpath, tsname))-ss Starting time
-t A few seconds after the start time , That is, the length of this segment
When capturing video -i and -ss The front and back position of the has a great influence .
-i In the first place, search according to the key frame , Fast .
-i After that, search frame by frame , Very slow , High precision , Suitable for small files .
For details, see :http://trac.ffmpeg.org/wiki/Seeking Click to open the link
summary :
1、 Generate... Manually m3u8 file , Need to get the total length of the file , Set the length of each clip .
filemsg = os.popen("ffprobe -v error -show_entries format=duration -print_format json %s" % srcvideopath).read()
print(filemsg)
length = float(json.loads(filemsg)['format']['duration'])
2、 Generate corresponding according to the request ts File and return .
边栏推荐
- Solve the timeout of dbeaver SQL client connection Phoenix query
- 单例模式3--单例模式
- CMake指定OpenCV版本
- 期货在线开户是否安全?去哪家公司手续费最低?
- Hbck 修复问题
- Understanding the execution order of T-SQL query from the execution order of join on and where
- 树莓派入门:树莓派的初始设置
- My creation anniversary
- Use the command to check the WiFi connection password under win10 system
- Spark SQL common time functions
猜你喜欢

Spark SQL空值Null,NaN判断和处理

基于OpenCV和YOLOv3的目标检测实例应用

Remember that spark foreachpartition once led to oom

Single or multiple human posture estimation using openpose

Pl/sql creates and executes ORALCE stored procedures and returns the result set

VS2010 add WAP mobile form template

Spark memory management mechanism new version

ice 100G 网卡分片报文 hash 问题

如何解决Visual Studio中scanf编译报错的问题

System. Accessviolationexception: an attempt was made to read or write to protected memory. This usually indicates that other memory is corrupted
随机推荐
从 join on 和 where 执行顺序认识T-sql查询执行顺序
mysql heap表_MySQL内存表heap使用总结-九五小庞
记一次Spark foreachPartition导致OOM
js URLEncode函数
防抖(debounce)和节流(throttle)
期货在线开户是否安全?去哪家公司手续费最低?
C language function review (pass value and address [binary search], recursion [factorial, Hanoi Tower, etc.))
Cmake specify opencv version
Idea remotely submits spark tasks to the yarn cluster
ios 面试题
Automatically set the template for VS2010 and add header comments
异步fifo的实现
推荐10个堪称神器的学习网站
Spark DF增加一列
Spark 内存管理机制 新版
How to solve the problem of scanf compilation error in Visual Studio
Solve the timeout of dbeaver SQL client connection Phoenix query
My creation anniversary
Hbck 修复问题
How much memory can a program use at most?