ffmpeg直播加水印、拉流等任务

2024-02-09 01:32

本文主要是介绍ffmpeg直播加水印、拉流等任务,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 直播流加水印

ffmpeg -i  https://outbound-production.explore.org/stream-production-319/.m3u8  -vf "movie=black_water.png[watermark]; [in][watermark] overlay=main_w-overlay_w-20:main_h-overlay_h-10 [out]" -c:v libx264 -c:a copy -f flv "rtmp://livepush.gonature.cn/live/qwee?txSecret=39a51cfacc1d5d93216add5b5e0a017e&txTime=60DD7A3B"

 定时任务拉流并保存MP4文件

const ffmpeg = require('fluent-ffmpeg')
const config = require('./live_catch_tasks.json')
const fs = require('fs')
ffmpeg.setFfmpegPath('/usr/local/ffmpeg/bin/ffmpeg')  // /Users/bin/Downloads/ffmpeg 本机
const oneDay = 86400000
const tasks = {}  // 正在执行的任务
const checkAndRun = async function(){console.log('check and run task...', new Date())// 遍历任务列表for(let i in config.tasks){const task = config.tasks[i]const now = new Date()// 取当前时间,格式如 00:00 const time = ('0'+now.getHours()).slice(-2) + ':' + ('0'+now.getMinutes()).slice(-2)const exists = fs.existsSync(config.storeDir + task.saveAs)let overwrite = falseif(exists){const filestat = fs.statSync(config.storeDir + task.saveAs) // console.log('now:',now.getDay(),time,filestat.mtimeMs)if((now.getTime() - filestat.mtimeMs) > oneDay){overwrite = true}}// console.log(now.getDay() === task.weekDay,time >= task.startTime,(!exists || (exists && overwrite)))if(!tasks[task.name] && now.getDay() === task.weekDay  // 判断是否为当天任务&& time >= task.startTime                          // 判断是否已到任务开始时间&& (!exists || (exists && overwrite))){            // 判断文件是否已存在 或 上一次更新时间距现在是否大于24小时((t)=> {tasks[t.name] = {id: t.id,name: t.name,saveAs: t.saveAs,status: null}// 创建 ffmpeg 任务tasks[t.name].process = ffmpeg(task.source)// .videoBitrate(1000).addOptions(t.ffmpegOptions).on('error', function(err, stdout, stderr) {console.error('record live error: ' + err.message)tasks[t.name].status = 'Error'}).on("start", function (commandLine) {console.log(commandLine) tasks[t.name].status = 'Going'}).on("end", function () {// console.log('push over.') delete tasks[t.name]}).save(config.storeDir + t.saveAs)})(task)}}
}
// 格式化输出到控制台
function log(){const list = Object.values(tasks).map(i=>{return { id: i.id, name: i.name, status: i.status || '', saveAs: i.saveAs}})list.sort((a,b)=> a.id - b.id )console.table(list)
}
function run(){checkAndRun()                    // 首次立即执行setInterval(checkAndRun, 30000)  // 每 30 秒检查一次setInterval(log, 5000)           // 日志每 5 秒输出一次
}
run()/*
const cmd = ffmpeg('/Users/bin/Downloads/movies/心火/Firelight.1997.1080p.WEBRip.x264-RARBG.mp4')
// .duration(60)
// .loop(10)
// .seekInput(834.5) // set start time
.videoBitrate(1000)
// .noAudio()
.addOptions(['-vcodec copy','-acodec copy','-bsf:a aac_adtstoasc']
)
// .output('/Users/bin/Downloads/outputfile.mp4')
// .output('rtmp://livepush.gonature.cn/live/live1?txSecret=d6f5ab25a7f845391b7b58dd274ef19e&txTime=858C3C88')
.on('error', function(err, stdout, stderr) {console.log('Cannot process video: ' + err.message);
})
.on('end', function() {console.log('Processing finished !');
})
.format("flv")
.output('rtmp://livepush.gonature.cn/live/live1?txSecret=d6f5ab25a7f845391b7b58dd274ef19e&txTime=858C3C88', {end: true
})
.on("start", function (commandLine) {console.log("pushing......") //开始推流
})
.on("error", function (err, stdout, stderr) {console.log('push error:', err) //推流失败
})
.on("end", function () {console.log('push over.') //推流结束
});cmd.run();
//.save('/Users/bin/Downloads/outputfile.mp4');
*/

node方式拉流推流并加水印

 var ffmpeg = require('fluent-ffmpeg');const command = ffmpeg('https://outbound-production.explore.org/stream-production-319/.m3u8').videoFilters("movie=black_water.png[watermark]; [in][watermark] overlay=main_w-overlay_w-20:main_h-overlay_h-10 [out]").addOptions(["-vcodec libx264","-preset ultrafast","-acodec aac"]).format("flv").output('rtmp://livepush.gonature.cn/live/qwee?txSecret=39a51cfacc1d5d93216add5b5e0a017e&txTime=60DD7A3B', {end: true}).on("start", function (commandLine) {console.log("commandLine: " + commandLine);}).on("error", function (err, stdout, stderr) {console.log("commandLine: " + '失败');}).on("end", function () {});command.run()

使用process_argv传递参数,执行固定的ID之间的任务

启动方式:

node manage.js 1 3
任务id 1-3,包含1和3

const ffmpeg = require('fluent-ffmpeg')
const config = require('./live_push_tasks.json')
const fs = require('fs')
// ffmpeg.setFfmpegPath('/usr/local/ffmpeg/bin/ffmpeg')  // /Users/bin/Downloads/ffmpeg 本机
const oneDay = 86400000
const tasks = {}  // 正在执行的任务
let begin = 1 // 开始初始值
let end = 100 // 结束初始值
process.argv.forEach(function(val, index, array) {console.log(index + ': ' + val);if (index === 2) {begin = val}if (index === 3) {end = val}
});
const checkAndRun = async function(){console.log('check and run task...', new Date())// 遍历任务列表for(let i in config.tasks){const task = config.tasks[i]const now = new Date()// console.log(now.getDay() === task.weekDay,time >= task.startTime,(!exists || (exists && overwrite)))if(!tasks[task.name] && begin <=task.id && end>=task.id){            // 判断文件是否已存在 或 上一次更新时间距现在是否大于24小时((t)=> {tasks[t.name] = {id: t.id,name: t.name,saveAs: t.saveAs,status: null}// 创建 ffmpeg 任务tasks[t.name].process = ffmpeg('https://outbound-production.explore.org/stream-production-'+task.source+'/.m3u8').videoFilters("movie=black_water.png[watermark]; [in][watermark] overlay=main_w-overlay_w-20:main_h-overlay_h-10 [out]").addOptions(["-vcodec libx264","-preset ultrafast","-acodec aac"]).format("flv").output(task.output, {end: true}).on("start", function (commandLine) {console.log(commandLine)tasks[t.name].status = 'Going'}).on("error", function (err, stdout, stderr) {console.error('record live error: ' + err.message)tasks[t.name].status = 'Error'}).on("end", function () {delete tasks[t.name]}).run();})(task)}}
}
// 格式化输出到控制台
function log(){const list = Object.values(tasks).map(i=>{return { id: i.id, name: i.name, status: i.status || ''}})list.sort((a,b)=> a.id - b.id )console.table(list)
}
function run(){checkAndRun()                    // 首次立即执行setInterval(checkAndRun, 30000)  // 每 30 秒检查一次setInterval(log, 5000)           // 日志每 5 秒输出一次
}
run()

添加码率

const ffmpeg = require('fluent-ffmpeg')
const config = require('./live_push_tasks.json')
const fs = require('fs')
// ffmpeg.setFfmpegPath('/usr/local/ffmpeg/bin/ffmpeg')  // /Users/bin/Downloads/ffmpeg 本机
const oneDay = 86400000
const tasks = {}  // 正在执行的任务
let begin = 1 // 开始初始值
let end = 100 // 结束初始值
process.argv.forEach(function(val, index, array) {console.log(index + ': ' + val);if (index === 2) {begin = val}if (index === 3) {end = val}
});
const checkAndRun = async function(){console.log('check and run task...', new Date())// 遍历任务列表for(let i in config.tasks){const task = config.tasks[i]const now = new Date()// console.log(now.getDay() === task.weekDay,time >= task.startTime,(!exists || (exists && overwrite)))if(!tasks[task.name] && begin <=task.id && end>=task.id){            // 判断文件是否已存在 或 上一次更新时间距现在是否大于24小时((t)=> {tasks[t.name] = {id: t.id,name: t.name,saveAs: t.saveAs,status: null}// 创建 ffmpeg 任务tasks[t.name].process = ffmpeg('https://outbound-production.explore.org/stream-production-'+task.source+'/.m3u8').videoFilters("movie=black_water.png[watermark]; [in][watermark] overlay=main_w-overlay_w-20:main_h-overlay_h-10 [out]").addOptions(["-vcodec libx264","-preset ultrafast","-acodec aac","-video_size 1920x1080"]).videoBitrate('3072k').format("flv").output(task.output, {end: true}).on("start", function (commandLine) {console.log(commandLine)tasks[t.name].status = 'Going'}).on("error", function (err, stdout, stderr) {console.error('record live error: ' + err.message)tasks[t.name].status = 'Error'}).on("end", function () {delete tasks[t.name]}).run();})(task)}}
}
// 格式化输出到控制台
function log(){const list = Object.values(tasks).map(i=>{return { id: i.id, name: i.name, status: i.status || ''}})list.sort((a,b)=> a.id - b.id )console.table(list)
}
function run(){checkAndRun()                    // 首次立即执行setInterval(checkAndRun, 30000)  // 每 30 秒检查一次setInterval(log, 5000)           // 日志每 5 秒输出一次
}
run()

这篇关于ffmpeg直播加水印、拉流等任务的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/692773

相关文章

Golang如何对cron进行二次封装实现指定时间执行定时任务

《Golang如何对cron进行二次封装实现指定时间执行定时任务》:本文主要介绍Golang如何对cron进行二次封装实现指定时间执行定时任务问题,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录背景cron库下载代码示例【1】结构体定义【2】定时任务开启【3】使用示例【4】控制台输出总结背景

在Golang中实现定时任务的几种高效方法

《在Golang中实现定时任务的几种高效方法》本文将详细介绍在Golang中实现定时任务的几种高效方法,包括time包中的Ticker和Timer、第三方库cron的使用,以及基于channel和go... 目录背景介绍目的和范围预期读者文档结构概述术语表核心概念与联系故事引入核心概念解释核心概念之间的关系

springboot如何通过http动态操作xxl-job任务

《springboot如何通过http动态操作xxl-job任务》:本文主要介绍springboot如何通过http动态操作xxl-job任务的问题,具有很好的参考价值,希望对大家有所帮助,如有错... 目录springboot通过http动态操作xxl-job任务一、maven依赖二、配置文件三、xxl-

Python中对FFmpeg封装开发库FFmpy详解

《Python中对FFmpeg封装开发库FFmpy详解》:本文主要介绍Python中对FFmpeg封装开发库FFmpy,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录一、FFmpy简介与安装1.1 FFmpy概述1.2 安装方法二、FFmpy核心类与方法2.1 FF

基于Linux的ffmpeg python的关键帧抽取

《基于Linux的ffmpegpython的关键帧抽取》本文主要介绍了基于Linux的ffmpegpython的关键帧抽取,实现以按帧或时间间隔抽取关键帧,文中通过示例代码介绍的非常详细,对大家的学... 目录1.FFmpeg的环境配置1) 创建一个虚拟环境envjavascript2) ffmpeg-py

一文详解MySQL如何设置自动备份任务

《一文详解MySQL如何设置自动备份任务》设置自动备份任务可以确保你的数据库定期备份,防止数据丢失,下面我们就来详细介绍一下如何使用Bash脚本和Cron任务在Linux系统上设置MySQL数据库的自... 目录1. 编写备份脚本1.1 创建并编辑备份脚本1.2 给予脚本执行权限2. 设置 Cron 任务2

Python使用FFmpeg实现高效音频格式转换工具

《Python使用FFmpeg实现高效音频格式转换工具》在数字音频处理领域,音频格式转换是一项基础但至关重要的功能,本文主要为大家介绍了Python如何使用FFmpeg实现强大功能的图形化音频转换工具... 目录概述功能详解软件效果展示主界面布局转换过程截图完成提示开发步骤详解1. 环境准备2. 项目功能结

SpringBoot使用ffmpeg实现视频压缩

《SpringBoot使用ffmpeg实现视频压缩》FFmpeg是一个开源的跨平台多媒体处理工具集,用于录制,转换,编辑和流式传输音频和视频,本文将使用ffmpeg实现视频压缩功能,有需要的可以参考... 目录核心功能1.格式转换2.编解码3.音视频处理4.流媒体支持5.滤镜(Filter)安装配置linu

Android NDK版本迭代与FFmpeg交叉编译完全指南

《AndroidNDK版本迭代与FFmpeg交叉编译完全指南》在Android开发中,使用NDK进行原生代码开发是一项常见需求,特别是当我们需要集成FFmpeg这样的多媒体处理库时,本文将深入分析A... 目录一、android NDK版本迭代分界线二、FFmpeg交叉编译关键注意事项三、完整编译脚本示例四

Django之定时任务django-crontab的实现

《Django之定时任务django-crontab的实现》Django可以使用第三方库如django-crontab来实现定时任务的调度,本文主要介绍了Django之定时任务django-cront... 目录crontab安装django-crontab注册应用定时时间格式定时时间示例设置定时任务@符号