ffmpeg_VideoTranscode.c 4.5 KB

/* Includes ------------------------------------------------------------------*/
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>

// 自建库 
#include "JZsdkLib.h"

// 拍照转码函数
T_JZsdkReturnCode FFMPEG_ShootPhotoTransCode(int width, int height)
{
    char ffmpegCmdStr[256];  //ffmpeg指令  
    int ret;
    FILE *NUMBER_SET_fp;

    //地址拼接
    char str1[40] = "/root/sdcard/DCIM/Photos/PSDK_PHOTO";
    char str2[6] = ".jpg";

	char *InPutPath =  "/root/sdcard/DCIM/Photos/temp.h264"; 
    char OutPath[256];
    int Numset;

    //从文件中读取序号
    NUMBER_SET_fp = fopen("/root/sdcard/DCIM/Photos/pthoto_number.txt","r");
    //如果没有记录文件
    if(NUMBER_SET_fp == NULL)
    {
        //新建一个记录文件,并写入0
        NUMBER_SET_fp = fopen("/root/sdcard/DCIM/Photos/pthoto_number.txt","w");
        fprintf(NUMBER_SET_fp, "%d", Numset);
    }   
    // 从文件中读取Numset,并自增1
    else 
    {
        fscanf(NUMBER_SET_fp, "%d", &Numset);
        Numset++;
        fclose(NUMBER_SET_fp);

        // 将新的LogNum写回文件中
        NUMBER_SET_fp = fopen("/root/sdcard/DCIM/Photos/pthoto_number.txt", "w");
        fprintf(NUMBER_SET_fp, "%d", Numset);
    }
    
    //关闭序号文件
    fclose(NUMBER_SET_fp);
    NUMBER_SET_fp = NULL;

    //拼接出输入地址
    snprintf(OutPath, 256, "%s_%05d%s", str1, Numset, str2);
    JZSDK_LOG_INFO("ShootPhoto_OutPath:%s",OutPath);

    //将h264照片转换为jpg格式
    snprintf(ffmpegCmdStr, 256,
             "echo \"y\" | ffmpeg -i \"%s\" -y -f image2 -t 0.001 -s %dx%d \"%s\" >/dev/null 2>&1", 
             InPutPath,
             width,
             height,
             OutPath);

    // snprintf(ffmpegCmdStr, 256,  
    //      "echo \"y\" | ffmpeg -ss 00:00:01 -i \"%s\" -y -vframes 1 \"%s\" >/dev/null 2>&1",   
    //      InPutPath,  
    //      OutPath);  
  
    JZSDK_LOG_INFO("cmdstr:%s",ffmpegCmdStr);

    //转码开始
    JZsdk_RunSystemCmd(ffmpegCmdStr);
}

// 录像转码函数
T_JZsdkReturnCode FFMPEG_RecordVideoTransCode()
{
    char ffmpegCmdStr[256];  //ffmpeg指令  
    int ret;
    FILE *NUMBER_SET_fp;

    //地址拼接
    char str1[40] = "/root/sdcard/DCIM/Videos/PSDK_VIDEO";
    char str2[6] = ".mp4";

	char *InPutPath =  "/root/sdcard/DCIM/Videos/temp.h264"; 
    char OutPath[256];
    int Numset;

    //从文件中读取序号
    NUMBER_SET_fp = fopen("/root/sdcard/DCIM/Videos/video_number.txt","r");
    //如果没有记录文件
    if(NUMBER_SET_fp == NULL)
    {
        //新建一个记录文件,并写入0
        NUMBER_SET_fp = fopen("/root/sdcard/DCIM/Videos/video_number.txt","w");
        fprintf(NUMBER_SET_fp, "%d", Numset);
    }   
    // 从文件中读取Numset,并自增1
    else 
    {
        fscanf(NUMBER_SET_fp, "%d", &Numset);
        Numset++;
        fclose(NUMBER_SET_fp);

        // 将新的LogNum写回文件中
        NUMBER_SET_fp = fopen("/root/sdcard/DCIM/Videos/video_number.txt", "w");
        fprintf(NUMBER_SET_fp, "%d", Numset);
    }
    
    //关闭序号文件
    fclose(NUMBER_SET_fp);
    NUMBER_SET_fp = NULL;

    //拼接出输入地址
    snprintf(OutPath, 256, "%s_%05d%s", str1, Numset, str2);
    JZSDK_LOG_INFO("RecordVideo_OutPath:%s",OutPath);

    //将h264照片转换为mp4格式
    snprintf(ffmpegCmdStr, 256,
             "echo \"y\" | ffmpeg -i \"%s\" -y -vcodec copy \"%s\" >/dev/null 2>&1",
             InPutPath,
             OutPath);

    JZSDK_LOG_INFO("cmdstr:%s",ffmpegCmdStr);

    //转码开始
    JZsdk_RunSystemCmd(ffmpegCmdStr);
}


// // 连拍转码函数 暂不使用
// static void ShootPhoroBurst_transcode()
// {
    
//     //ffmpeg指令
//     char ffmpegCmdStr[FFMPEG_CMD_BUF_SIZE];    
//     char *H3_ShootPhoroBurst_InPath =  "/root/sdcard/DCIM/Videos/temp.h264"; 
//     char H3_ShootPhoroBurst_OutPath[FFMPEG_CMD_BUF_SIZE];   


//     //转码
//     char str1[40] = "/root/sdcard/DCIM/Photos/video_";
//     char str2[10] = ".mp4";

//     //根据时间写名字
//     snprintf(H3_ShootPhoroBurst_OutPath, 256, "%s_%d_%d%s", str1,GPS_Receive_Date,GPS_Receive_Time,str2);

//     //将h264照片转换为jpg格式
//     snprintf(ffmpegCmdStr, FFMPEG_CMD_BUF_SIZE,
//              "echo \"y\" | ffmpeg -i \"%s\" -r 25 -f image2 -t 0.001 -s 1280*720 \"%s\" >/dev/null 2>&1", 
//              H3_ShootPhoroBurst_InPath,
//              H3_ShootPhoroBurst_OutPath);

//     //ffmpeg -i output.mp4 -r 30 -f image2 foo-%05d.jpeg
//     USER_LOG_INFO("录像保存位置:%s",H3_ShootPhoroBurst_OutPath);

    
//     RunSystemCmd(ffmpegCmdStr);
    
// }