A script for splitting videos using ffmpeg v2
//author:hnrayer@gmail.com
#if _MSC_VER
#define inline __inline
#endif
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libavutil/log.h>
#include <stdio.h>
#include <stdlib.h>
#pragma warning(disable : 4996)
int main(int argc, char **argv)
{
AVFormatContext *piFormatCtx = NULL;
//the number of segments
char *pszSegments = NULL;
int i_segments;
//the duration of each segment
int sum_secs = 0;
int step = 0;
char szcmd[128];//command
char *pszFullname;
char drive[_MAX_DRIVE];
char dir[_MAX_DIR];
char fname[_MAX_FNAME];
char ext[_MAX_EXT];
if (argc < 3)
{
printf("usage: ffsplit infile num [num:the number of segments]\n");
return -1;
}
pszSegments = argv[2];
i_segments = atoi(pszSegments);
//Register all formats and codecs
av_register_all();
//Open video file
if(avformat_open_input(&piFormatCtx, argv[1], NULL, NULL) != 0)
{
return -1;
}
//Retrieve stream information
if(avformat_find_stream_info(piFormatCtx, NULL) < 0)
{
return -1;
}
//print the info of duration
av_log(NULL, AV_LOG_INFO, " Duration: ");
if (piFormatCtx->duration != AV_NOPTS_VALUE)
{
int hours, mins, secs, us;
int64_t duration = piFormatCtx->duration + 5000;
secs = duration / AV_TIME_BASE;
us = duration % AV_TIME_BASE;
mins = secs / 60;
secs %= 60;
hours = mins / 60;
mins %= 60;
av_log(NULL, AV_LOG_INFO, "%02d:%02d:%02d.%02d\n", hours, mins, secs, (100 * us) / AV_TIME_BASE);
sum_secs = hours*60*60 + mins*60 +secs;
}
else
{
av_log(NULL, AV_LOG_INFO, "N/A");
}
step = sum_secs / i_segments;
pszFullname = piFormatCtx->filename;
_splitpath(pszFullname, drive, dir, fname, ext);
/////////////////////////////////////////
sprintf(szcmd, "ffmpeg -i %s -codec copy -map 0 -f segment -segment_time %d %s_%%03d%s", argv[1], step, fname, ext);
puts(szcmd);
system(szcmd);
////////////////////////////////////////
//Close the video file
avformat_close_input(&piFormatCtx);
return 0;
}
时间宝贵,只能复制+粘贴,若图片无法显示或排版混乱,请访问https://elesos.github.io查找原文
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 字符编码:从基础到乱码解决