Skip to content

Commit

Permalink
fix: video duration accuracy
Browse files Browse the repository at this point in the history
  • Loading branch information
mengxiaoxuan committed May 22, 2024
1 parent e0dc3af commit e45d8db
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
* 编译前需要下载 [ffmpeg-3.4.8.tar.xz](http://ffmpeg.org/releases/ffmpeg-3.4.8.tar.xz) 并解压至 `script` 目录下方。

* emcc下载:git clone https://github.com/emscripten-core/emsdk.git
* 安装emsdk,在emsdk目录下执行

```shell
git pull
Expand All @@ -52,7 +53,7 @@
* `emsdk` 需要安装于代码库根目录

## 编译脚本

* 安装依赖 `npm i`,执行script目录下的脚本
`script`下面是编译脚本
`build_ffmpeg-3.4.8.sh` 编译本地c编译环境的ffmpeg的lib库
`build-emcc.sh` 编译emcc环境需要的ffmpeg的lib库,编译结果lib/ffmpeg-emcc
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cheetah-capture",
"version": "0.1.5",
"version": "0.1.6",
"description": "cheetah-capture是基于ffmpeg的wasm截取视频帧工具",
"keywords": [
"ffmpeg",
Expand Down
21 changes: 20 additions & 1 deletion src/capture.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <libavformat/avformat.h>
#include <libavutil/imgutils.h>
#include <libswscale/swscale.h>
#include <libavutil/avutil.h>

typedef struct
{
Expand All @@ -19,6 +20,10 @@ typedef struct
int lastIframe;
} FrameInfo;

// double av_q2d(AVRational r) {
// return r.num / (double) r.den;
// }

AVFrame *initAVFrame(AVCodecContext *pCodecCtx, uint8_t **frameBuffer)
{
AVFrame *pFrameRGB = av_frame_alloc();
Expand Down Expand Up @@ -227,7 +232,21 @@ int videoStream, int time, FrameInfo *frameInfo, int *Iframe, int counts, int id
imageData = (ImageData *)malloc(sizeof(ImageData));
imageData->width = (uint32_t)pNewCodecCtx->width;
imageData->height = (uint32_t)pNewCodecCtx->height;
imageData->duration = (uint32_t)pFormatCtx->duration;
// 计算视频的实际时长
if(pFormatCtx->duration!=AV_NOPTS_VALUE){
int hours,mins,secs,us;
int64_t duration=pFormatCtx->duration+5000;
secs=duration/AV_TIME_BASE;
us=duration%AV_TIME_BASE;//1000000
mins=secs/60;
secs%=60;
hours=mins/60;
mins%=60;
int totalMilliseconds = (hours * 3600000) + (mins * 60000) + (secs * 1000) + (100 * us / AV_TIME_BASE);
imageData->duration = (uint32_t)totalMilliseconds;
} else {
imageData->duration = 0;
}
imageData->data = getFrameBuffer(pFrameRGB, pNewCodecCtx);
emscripten_run_script(get_js_code(*imageData, id));
av_frame_free(&pFrameRGB);
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ export async function initCapture({
const img = await getUrl(width, height, imageDataBuffer, angle);
const cbk = pool.getCbk(id);
const {onChange} = cbk;
const info = {width, height, duration: duration / 1000000};
const info = {width, height, duration: Number(duration) / 1000};
const {url} = pool.getCbk(id);
onChange && onChange({url}, img, info);
url.push(img.url);
Expand Down

0 comments on commit e45d8db

Please sign in to comment.