Skip to content

Commit

Permalink
fix: utf8 encode
Browse files Browse the repository at this point in the history
  • Loading branch information
mengxiaoxuan committed Jun 6, 2024
1 parent 655cdd2 commit c598dc4
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 4 deletions.
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.2.0",
"version": "0.2.0-beta.3",
"description": "cheetah-capture是基于ffmpeg的wasm截取视频帧工具",
"keywords": [
"ffmpeg",
Expand Down
58 changes: 55 additions & 3 deletions src/capture.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <libavutil/imgutils.h>
#include <libswscale/swscale.h>
#include <libavutil/avutil.h>
#include <stdio.h>
#include <stdlib.h>

typedef struct
{
Expand All @@ -24,6 +26,40 @@ typedef struct
// return r.num / (double) r.den;
// }

char* iso8859_1_to_utf8(const char* input) {
if (input == NULL) return NULL;

// 计算 UTF-8 字符串的长度
size_t len = 0;
for (const char* p = input; *p != '\0'; p++) {
unsigned char ch = (unsigned char)*p;
len += (ch < 128) ? 1 : 2; // 128-255 范围的字符需要两个字节
}

// 分配内存(+1 为了 null terminator)
char* output = (char*)malloc(len + 1);
if (output == NULL) {
perror("malloc failed");
return NULL;
}

// 填充输出字符串
char* out_ptr = output;
for (const char* p = input; *p != '\0'; p++) {
unsigned char ch = (unsigned char)*p;
if (ch < 128) {
*out_ptr++ = ch;
} else {
*out_ptr++ = 0xC0 | (ch >> 6); // 前两位标记字节
*out_ptr++ = 0x80 | (ch & 0x3F); // 后六位数据字节
}
}
*out_ptr = '\0'; // null-terminated

return output;
}


AVFrame *initAVFrame(AVCodecContext *pCodecCtx, uint8_t **frameBuffer)
{
AVFrame *pFrameRGB = av_frame_alloc();
Expand All @@ -48,9 +84,11 @@ static char *dump_metadata(void *ctx, AVDictionary *m, const char *indent, const
char *info = def;
AVDictionaryEntry *tag = NULL;
AVDictionaryEntry *result = av_dict_get(m, indent, NULL, 0);

printf("遍历到的内容%s,值是%s\n", indent, result->value);
if (result->value) {
return result->value;
return iso8859_1_to_utf8(result->value);
// return result->value;
}
return info;
}
Expand Down Expand Up @@ -554,6 +592,7 @@ ImageData **captureByMs(char *ms, char *path, int id)
avformat_close_input(&pFormatCtx);
return dataList;
}

// 获取视频的元数据
char *getMetaDataByKey(const char *key, const char *path) {
AVFormatContext *pFormatCtx = avformat_alloc_context();
Expand All @@ -573,6 +612,7 @@ char *getMetaDataByKey(const char *key, const char *path) {
}

const char *value = dump_metadata(NULL, pFormatCtx->metadata, key, "");
printf("===>得到的value: %s\n", value);
avformat_free_context(pFormatCtx);

if (!value) {
Expand All @@ -581,8 +621,20 @@ char *getMetaDataByKey(const char *key, const char *path) {
}

// 创建一个字符串副本,因为原始值可能随 pFormatCtx 释放而失效
char *result = strdup(value);
return result;
// char *result = strdup(value);
return value;
}
size_t get_string_length(int ptr) {
if (ptr == 0) {
printf("Received null pointer\n");
return 0; // 检查空指针
}
char* str = (char*) ptr; // 将整数地址转换为字符指针
printf("Received non-null pointer, pointing to: %s\n", str); // 打印指针指向的字符串

size_t length = strlen(str); // 使用 strlen 获取字符串长度
printf("String length: %zu\n", length);
return length;
}
int main(int argc, char const *argv[])
{
Expand Down
24 changes: 24 additions & 0 deletions src/capture.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,30 @@ class ImageCapture {
meta: metadataValue,
id,
});
// // cGetMetadata();
// const ptr = Module.ccall('getMetaDataByKey', 'number', ['string', 'string', 'number'], [key, `${this.path}/${this.name}`, id]);
// if (!ptr) {
// console.error('Invalid pointer received');
// } else {
// console.log('===>ptr', ptr);
// const length = Module.ccall('get_string_length', 'number', ['number'], [ptr]);
// console.log('===>length', length);
// if (length > 0) {
// const data = new Uint8Array(Module.HEAPU8.buffer, ptr, length);
// const decoder = new TextDecoder('utf-8');
// const metadataValue = decoder.decode(data);
// console.log('==>from worker', metadataValue);
// self.postMessage({
// type: Events.getMetadataOnSuccess,
// meta: metadataValue,
// id,
// });
// } else {
// console.error('Received empty or invalid length');
// }
// }


}
}

Expand Down

0 comments on commit c598dc4

Please sign in to comment.