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

相关文章

SpringBoot集成XXL-JOB实现任务管理全流程

《SpringBoot集成XXL-JOB实现任务管理全流程》XXL-JOB是一款轻量级分布式任务调度平台,功能丰富、界面简洁、易于扩展,本文介绍如何通过SpringBoot项目,使用RestTempl... 目录一、前言二、项目结构简述三、Maven 依赖四、Controller 代码详解五、Service

Linux系统管理与进程任务管理方式

《Linux系统管理与进程任务管理方式》本文系统讲解Linux管理核心技能,涵盖引导流程、服务控制(Systemd与GRUB2)、进程管理(前台/后台运行、工具使用)、计划任务(at/cron)及常用... 目录引言一、linux系统引导过程与服务控制1.1 系统引导的五个关键阶段1.2 GRUB2的进化优

基于Java和FFmpeg实现视频压缩和剪辑功能

《基于Java和FFmpeg实现视频压缩和剪辑功能》在视频处理开发中,压缩和剪辑是常见的需求,本文将介绍如何使用Java结合FFmpeg实现视频压缩和剪辑功能,同时去除数据库操作,仅专注于视频处理,需... 目录引言1. 环境准备1.1 项目依赖1.2 安装 FFmpeg2. 视频压缩功能实现2.1 主要功

Python Flask实现定时任务的不同方法详解

《PythonFlask实现定时任务的不同方法详解》在Flask中实现定时任务,最常用的方法是使用APScheduler库,本文将提供一个完整的解决方案,有需要的小伙伴可以跟随小编一起学习一下... 目录完js整实现方案代码解释1. 依赖安装2. 核心组件3. 任务类型4. 任务管理5. 持久化存储生产环境

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