php中文网 | cnphp.com

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 153|回复: 0

使用mp4v2将h264和aac封装成mp4

[复制链接]

2643

主题

2650

帖子

9398

积分

管理员

Rank: 9Rank: 9Rank: 9

UID
1
威望
0
积分
6633
贡献
0
注册时间
2021-4-14
最后登录
2024-5-3
在线时间
670 小时
QQ
发表于 2023-9-30 15:52:54 | 显示全部楼层 |阅读模式
  1. #include<stdio.h>
  2. #include<stdint.h>
  3. #include "mp4v2/mp4v2.h"
  4. #include"AacADTSHeader.h"
  5. #include"NaluParse.h"
  6. uint16_t  GetDecoderSpecificInfo(uint8_t audioObjectType, uint8_t sampleRateIndex, uint8_t channelNumber)
  7. {
  8.         uint16_t decoderSpecificInfo = 0;
  9.         uint8_t* p = (uint8_t*)&decoderSpecificInfo;
  10.         p[0] = ((audioObjectType << 3) & 0xf8) | ((sampleRateIndex >> 1) & 0x07);
  11.         p[1] = ((sampleRateIndex << 7) & 0x80) | ((channelNumber << 3) & 0x70);
  12.         return decoderSpecificInfo;
  13. }

  14. int main(int argc, char* argv[])
  15. {
  16.         MP4FileHandle mp4 = NULL;
  17.         FILE* h264=NULL;
  18.         FILE* aac = NULL;
  19.         MP4TrackId videoTrack = MP4_INVALID_TRACK_ID;
  20.         MP4TrackId audioTrack = MP4_INVALID_TRACK_ID;
  21.         try {
  22.                 AC::NaluParse naluParse;
  23.                 AC::Nalu nalu;
  24.                 AC::AacADTSHeader adtsHeader;
  25.                 unsigned char bufferFull[1028];
  26.                 unsigned char* buf = bufferFull + 4;
  27.                 int size;
  28.                 int naluType;
  29.                 unsigned char* aacData;
  30.                 int aacDataLength;
  31.                 int width = 576;
  32.                 int height = 320;
  33.                 double frameRate = 29.97;
  34.                 int timeScale = 90000;
  35.                 int sampling_frequency_set[] = { 6000   ,88200  ,64000  ,48000  ,44100  ,32000  ,24000  ,22050  ,16000  ,12000  , 11025 , 8000  , 7350 };
  36.                 mp4 = MP4Create("test.mp4", 0);
  37.                 if (mp4 == MP4_INVALID_FILE_HANDLE)
  38.                 {
  39.                         throw std::exception("Create mp4 handle fialed.\n");
  40.                 }
  41.                 h264 = fopen("test.h264", "rb+");
  42.                 if (!h264)
  43.                 {
  44.                         throw std::exception("Opene h264 handle fialed.\n");

  45.                 }
  46.                 aac = fopen("test.aac", "rb+");
  47.                 if (!aac)
  48.                 {
  49.                         throw std::exception("Opene aac handle fialed.\n");

  50.                 }
  51.                 //  Ƶ
  52.                 while (1)
  53.                 {
  54.                         size = fread(buf, 1, 1024, h264);
  55.                         if (size < 1)
  56.                                 break;
  57.                         naluParse.SendH264Stream(buf, size);
  58.                         while (naluParse.ReceiveNalu(nalu))
  59.                         {
  60.                                 bool isIdr = true;
  61.                                 naluType = nalu.GetData()[0] & 0x1F;
  62.                                 switch (naluType)
  63.                                 {
  64.                                 case 01:
  65.                                 case 02:
  66.                                 case 03:
  67.                                 case 04:
  68.                                         isIdr = false;
  69.                                 case 05://idr
  70.                                 {
  71.                                         auto pNalu = nalu.GetData();
  72.                                         pNalu -= 4;
  73.                                         pNalu[0] = (nalu.GetDataLength() >> 24) & 0xFF;
  74.                                         pNalu[1] = (nalu.GetDataLength() >> 16) & 0xFF;
  75.                                         pNalu[2] = (nalu.GetDataLength() >> 8) & 0xFF;
  76.                                         pNalu[3] = (nalu.GetDataLength() >> 0) & 0xFF;
  77.                                         if (!MP4WriteSample(mp4, videoTrack, pNalu, nalu.GetDataLength() + 4, MP4_INVALID_DURATION, 0, isIdr))
  78.                                         {
  79.                                                 printf("Error:Can't write sample.\n");
  80.                                         }
  81.                                 }
  82.                                 break;
  83.                                 case 7: // SPS
  84.                                 {
  85.                                         if (videoTrack == MP4_INVALID_TRACK_ID)
  86.                                         {
  87.                                                 videoTrack = MP4AddH264VideoTrack
  88.                                                 (mp4,
  89.                                                         timeScale,
  90.                                                         timeScale / frameRate,
  91.                                                         width,
  92.                                                         height,
  93.                                                         nalu.GetData()[1],
  94.                                                         nalu.GetData()[2],
  95.                                                         nalu.GetData()[3],
  96.                                                         3);                     // 4 bytes length before each NAL unit
  97.                                                 if (videoTrack == MP4_INVALID_TRACK_ID)
  98.                                                 {
  99.                                                         printf("Error:Can't add track.\n");
  100.                                                         return -1;
  101.                                                 }
  102.                                                 MP4SetVideoProfileLevel(mp4, 0x7F);
  103.                                                 MP4AddH264SequenceParameterSet(mp4, videoTrack, nalu.GetData(), nalu.GetDataLength());
  104.                                         }
  105.                                 }
  106.                                 break;
  107.                                 case 8: // PPS
  108.                                 {
  109.                                         MP4AddH264PictureParameterSet(mp4, videoTrack, nalu.GetData(), nalu.GetDataLength());
  110.                                 }
  111.                                 break;
  112.                                 }
  113.                         }
  114.                 }
  115.                 //  Ƶ
  116.                 while (1)
  117.                 {
  118.                         int size;
  119.                         size = fread(buf, 1, 7, aac);
  120.                         if (size < 7)
  121.                                 break;
  122.                         AC::AacADTSParse::BinaryToHeader(buf, adtsHeader);
  123.                         size = fread(buf, 1, adtsHeader.aac_frame_length - 7, aac);
  124.                         if (size != adtsHeader.aac_frame_length - 7)
  125.                         {
  126.                                 throw std::exception("incorrect length!");
  127.                         }
  128.                         if (adtsHeader.protection_absent == 0)
  129.                                 //  У  λ
  130.                         {
  131.                                 aacData = buf + 2;
  132.                                 aacDataLength = size - 2;
  133.                                 //TODO      У  λbuffer[0]  buffer[1]
  134.                         }
  135.                         else
  136.                         {
  137.                                 aacData = buf;
  138.                                 aacDataLength = size;
  139.                         }
  140.                         //TODO      ȡ  aac    aacData  aacDataLength
  141.                         if (audioTrack == MP4_INVALID_TRACK_ID)
  142.                         {
  143.                                 audioTrack = MP4AddAudioTrack(mp4, sampling_frequency_set[adtsHeader.sampling_frequency_index], 1024, MP4_MPEG4_AUDIO_TYPE);
  144.                                 if (audioTrack == MP4_INVALID_TRACK_ID)
  145.                                 {
  146.                                         throw std::exception("Add audio track failed!");
  147.                                 }
  148.                                 MP4SetAudioProfileLevel(mp4, 0x02);
  149.                                 auto config = GetDecoderSpecificInfo(adtsHeader.profile + 1, adtsHeader.sampling_frequency_index, adtsHeader.channel_configuration);
  150.                                 if (!MP4SetTrackESConfiguration(mp4, audioTrack, (uint8_t*)&config, 2))
  151.                                 {
  152.                                         throw std::exception("set config failed!");
  153.                                 }
  154.                         }
  155.                         MP4WriteSample(mp4, audioTrack, aacData, aacDataLength, MP4_INVALID_DURATION, 0, 1);
  156.                 }
  157.         }
  158.         catch (const std::exception& e)
  159.         {
  160.                 printf("%s,\n", e.what());
  161.         }
  162.         if (aac)
  163.                 fclose(aac);
  164.         if (h264)
  165.                 fclose(h264);
  166.         if (mp4)
  167.                 MP4Close(mp4);
  168.         return 0;
  169. }
  170. /*FILE* f = fopen("test_music.aac", "rb+");
  171. uint8_t buffer[1024];
  172. AC::AacADTSHeader h;
  173. while (1)
  174. {
  175.         int size;
  176.         size = fread(buffer, 1, 7, f);
  177.         if (size < 7)
  178.                 break;
  179.         h.LoadtData(buffer);
  180.         h.SaveData(buffer);
  181.         AC::AacADTSHeader h2;
  182.         h2.LoadtData(buffer);
  183.         size = fread(buffer, 1, h2.GetAacFrameLength() - 7, f);
  184.         if (size != h2.GetAacFrameLength() - 7)
  185.         {
  186.                 throw;
  187.         }

  188. }*/


  189. //int main(int argc, char* argv[])
  190. //{
  191. //        GdiScreenCaptureTest();
  192. //        MP4FileHandle pHandle = NULL;
  193. //        FILE* f;
  194. //        MP4TrackId videoId = MP4_INVALID_TRACK_ID;
  195. //
  196. //        int width = 1920;
  197. //        int height = 1080;
  198. //        int frameRate = 20;
  199. //        int timeScale = 90000;
  200. //        unsigned char fullBuffer[8196] = {0,0,0,0,0};
  201. //        unsigned char *buffer= fullBuffer+4;
  202. //        unsigned char* pNalu;
  203. //        unsigned char naluType;
  204. //        bool isIdr = true;
  205. //        f = fopen("test.h264", "rb+");
  206. //        if (!f)
  207. //        {
  208. //                printf("ERROR:Open h264 file fialed.\n");
  209. //                return -1;
  210. //        }
  211. //        for (int i = 1; i <= 8192; i++)
  212. //        {
  213. //
  214. //                AC::NaluParse parse;
  215. //                pHandle = MP4Create("test1.mp4", 0);
  216. //                if (pHandle == MP4_INVALID_FILE_HANDLE)
  217. //                {
  218. //                        printf("ERROR:Create mp4 handle fialed.\n");
  219. //                        return -1;
  220. //                }
  221. //                while (1)
  222. //                {
  223. //                        int size = fread(buffer, 1, i, f);
  224. //                        if (size < 1)
  225. //                        {
  226. //                                break;
  227. //                        }
  228. //                        parse.SendH264Stream(buffer, size);
  229. //                        AC::Nalu nalu;
  230. //                        while (parse.ReceiveNalu(nalu))
  231. //                        {
  232. //                                pNalu = nalu.GetData();
  233. //                                naluType = pNalu[0] & 0x1F;
  234. //                                switch (naluType)
  235. //                                {
  236. //                                case 01:
  237. //                                case 02:
  238. //                                case 03:
  239. //                                case 04:
  240. //                                        isIdr = false;
  241. //                                case 05://idr
  242. //                                {
  243. //                                        pNalu -= 4;
  244. //                                        pNalu[0] = (nalu.GetDataLength() >> 24) & 0xFF;
  245. //                                        pNalu[1] = (nalu.GetDataLength() >> 16) & 0xFF;
  246. //                                        pNalu[2] = (nalu.GetDataLength() >> 8) & 0xFF;
  247. //                                        pNalu[3] = (nalu.GetDataLength() >> 0) & 0xFF;
  248. //                                        if (!MP4WriteSample(pHandle, videoId, pNalu, nalu.GetDataLength() + 4, MP4_INVALID_DURATION, 0, isIdr))
  249. //                                        {
  250. //                                                printf("Error:Can't write sample.\n");
  251. //                                        }
  252. //                                        isIdr = true;
  253. //                                }
  254. //                                break;
  255. //                                case 7: // SPS
  256. //                                {
  257. //                                        if (videoId == MP4_INVALID_TRACK_ID)
  258. //                                        {
  259. //                                                videoId = MP4AddH264VideoTrack
  260. //                                                (pHandle,
  261. //                                                        timeScale,
  262. //                                                        timeScale / frameRate,
  263. //                                                        width,
  264. //                                                        height,
  265. //                                                        pNalu[1],
  266. //                                                        pNalu[2],
  267. //                                                        pNalu[3],
  268. //                                                        3);
  269. //                                                if (videoId == MP4_INVALID_TRACK_ID)
  270. //                                                {
  271. //                                                        printf("Error:Can't add track.\n");
  272. //                                                        return -1;
  273. //                                                }
  274. //                                                MP4AddH264SequenceParameterSet(pHandle, videoId, pNalu, nalu.GetDataLength());
  275. //                                        }
  276. //                                }
  277. //                                break;
  278. //                                case 8: // PPS
  279. //                                {
  280. //                                        MP4AddH264PictureParameterSet(pHandle, videoId, pNalu, nalu.GetDataLength());
  281. //                                }
  282. //                                break;
  283. //                                default:
  284. //                                        break;
  285. //                                }
  286. //                        }
  287. //                }
  288. //                MP4Close(pHandle);
  289. //                fseek(f, 0, SEEK_SET);
  290. //                videoId = MP4_INVALID_TRACK_ID;
  291. //                if (!FileHelper::Compare("test.mp4", "test1.mp4"))
  292. //                {
  293. //                        throw "error";
  294. //                }
  295. //        }
  296. //
  297. //        return 0;
  298. //
  299. //
  300. //
  301. //        try {
  302. //                WNDCLASS wndclass;
  303. //                wndclass.style = CS_HREDRAW | CS_VREDRAW;
  304. //                wndclass.lpfnWndProc = WndProc;
  305. //                wndclass.cbClsExtra = 0;
  306. //                wndclass.cbWndExtra = 0;
  307. //                wndclass.hInstance = GetModuleHandle(NULL);
  308. //                wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  309. //                wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  310. //                wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
  311. //                wndclass.lpszMenuName = 0;
  312. //                wndclass.lpszClassName = L"DemoWindow";
  313. //                if (!RegisterClass(&wndclass))
  314. //                {
  315. //                        MessageBox(NULL, TEXT("        ʧ  !"), L"", MB_ICONERROR);
  316. //                }
  317. //                auto title = L"       ";
  318. //                HWND        hwnd = CreateWindow(
  319. //                        L"DemoWindow",
  320. //                        title,
  321. //                        WS_OVERLAPPEDWINDOW,
  322. //                        0,
  323. //                        0,
  324. //                        1280,
  325. //                        720,
  326. //                        NULL,
  327. //                        NULL,
  328. //                        GetModuleHandle(NULL),
  329. //                        NULL
  330. //                );
  331. //                ShowWindow(hwnd, SW_SHOW);
  332. //
  333. //                AC::SdlVideoRender sr;
  334. //                sr.SetFormat(AC::PixelFormat::PIXELFORMAT_NV12);
  335. //                sr.SetWindow(hwnd);
  336. //                sr.Initial();
  337. //                while (1)
  338. //                {
  339. //                        auto d = AC::MFCameraCapture::GetDevices();
  340. //                        AC::MFCameraCapture mcc(d[0].Id);
  341. //                        for (auto i : d[0].SurportForamts)
  342. //                        {
  343. //                                if (i.PixelFormat == AC::PixelFormat::PIXELFORMAT_NV12 && i.Width == 1280 && i.Height == 720)
  344. //                                {
  345. //                                        mcc.SetVideoFormat(i);
  346. //                                        break;
  347. //                                }
  348. //                        }
  349. //                        mcc.PixelDataArrived += [&](auto s, AC::IVideoInput::PixelDataArrivedEventArgs* e) {
  350. //                                sr.Present(e->Frame.Data, e->Frame.DataLength, e->Frame.Width, e->Frame.Height, 0, 0, e->Frame.Width, e->Frame.Height, 0, 0, e->Frame.Width, e->Frame.Height);
  351. //                        };
  352. //                        mcc.Open();
  353. //                        Sleep(50);
  354. //                }
  355. //                MSG msg;
  356. //                HACCEL hAccelTable = NULL;
  357. //                //     Ϣѭ  :
  358. //                while (GetMessage(&msg, NULL, 0, 0))
  359. //                {
  360. //                        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
  361. //                        {
  362. //                                TranslateMessage(&msg);
  363. //                                DispatchMessage(&msg);
  364. //                        }
  365. //                }
  366. //        }
  367. //        catch (const std::exception& e) {
  368. //                printf("%s", e.what());
  369. //        }
  370. //        //
  371. //
  372. //        //GdiScreenCaptureTest();
  373. //
  374. //        return 0;
  375. //}



  376. ////      
  377. //
  378. //static int getNextNalu(unsigned char* h264Frame, int len, int& offset)
  379. //{
  380. //        int oldOffset = offset;
  381. //        int spliterLen = 4;
  382. //        offset += 3;
  383. //        while (offset < len)
  384. //        {
  385. //                if (h264Frame[offset - 3] == 0 && h264Frame[offset - 2] == 0 && h264Frame[offset - 1] == 1)
  386. //                {
  387. //                        if (offset > 3) {
  388. //                                if (h264Frame[offset - 4] == 0) {
  389. //                                        spliterLen = 4;
  390. //                                }
  391. //                                else
  392. //                                {
  393. //                                        spliterLen = 3;
  394. //                                }
  395. //                        }
  396. //                        else {
  397. //                                spliterLen = 3;
  398. //                        }
  399. //                        break;
  400. //                }
  401. //                offset++;
  402. //        }
  403. //
  404. //        return offset - oldOffset - spliterLen;
  405. //}
  406. //double atime = 0;
  407. //
  408. //int GetClock() {
  409. //        return atime;
  410. //
  411. //}
  412. //
  413. //MP4FileHandle pHandle = NULL;
  414. //AC::SdlVideoRender sr;
  415. //AC::IntelHardwareEncoder* he;
  416. //MP4TrackId videoId;
  417. //int addStream = 1;
  418. //FILE* fh264 = NULL;
  419. //int64_t _timestamp = 0;
  420. //
  421. //class A :public AC::IVideoCaptureCallback
  422. //{
  423. //        // ͨ   IVideoCaptureCallback  ̳
  424. //        virtual void OnCaptrue(const unsigned char* buf, int len, int width, int height, AC::PixelFormat format, unsigned int timestamp) override
  425. //        {
  426. //                //sr.Present(buf, len, width, height, 0, 0, width, height, 0, 0, 640, 360);
  427. //                try {
  428. //
  429. //                        if (!he)
  430. //                        {
  431. //                                AC::EncodeParam param;
  432. //                                param.Bitrate = 1024;
  433. //                                param.Framerate = 20;
  434. //                                param.Width = 1920;
  435. //                                param.Height = 1080;
  436. //                                //param.Profile = AC::PROFILE_AVC_BASELINE;
  437. //                                //param.GopSize = 50;
  438. //                                he = new AC::IntelHardwareEncoder(param);
  439. //
  440. //                                he->EncodedDataArrived += [&](auto s, auto e) {
  441. //
  442. //                                        if (!fh264)
  443. //                                                fh264 = fopen("test.h264", "wb+");
  444. //                                        fwrite(e->Frame.Data, 1, e->Frame.DataLength, fh264);
  445. //                                        unsigned char* pBuf = e->Frame.Data;
  446. //                                        unsigned char* pNalu = e->Frame.Data;
  447. //                                        unsigned char naluType;
  448. //                                        int len = 0;
  449. //                                        int offset = 0;
  450. //                                        int num = 0;
  451. //                                        int width = 1920;
  452. //                                        int height = 1080;
  453. //                                        int frameRate = 20;
  454. //                                        int timeScale = 90000;
  455. //                                        bool isIdr = true;
  456. //                                        while (1)
  457. //                                        {
  458. //                                                len = getNextNalu(pBuf, e->Frame.DataLength, offset);
  459. //                                        gotNal:
  460. //                                                if (len < 0)
  461. //                                                        break;
  462. //                                                pNalu = pBuf + offset;
  463. //                                                naluType = pNalu[0] & 0x1F;
  464. //                                                switch (naluType)
  465. //                                                {
  466. //                                                case 01:
  467. //                                                case 02:
  468. //                                                case 03:
  469. //                                                case 04:
  470. //                                                        isIdr = false;
  471. //                                                case 05://idr
  472. //                                                {
  473. //                                                        len = e->Frame.DataLength - offset;
  474. //                                                        if (offset < 4)
  475. //                                                                break;
  476. //                                                        pNalu -= 4;
  477. //                                                        pNalu[0] = (len >> 24) & 0xFF;
  478. //                                                        pNalu[1] = (len >> 16) & 0xFF;
  479. //                                                        pNalu[2] = (len >> 8) & 0xFF;
  480. //                                                        pNalu[3] = (len >> 0) & 0xFF;
  481. //
  482. //
  483. //                                                        auto _duration = e->Frame.Timestamp - _timestamp;
  484. //                                                        _timestamp = e->Frame.Timestamp;
  485. //                                                        printf("%d \n", _duration * timeScale / 1000);
  486. //                                                        if (!MP4WriteSample(pHandle, videoId, pNalu, len + 4, _duration * timeScale / 1000, 0, isIdr))
  487. //                                                        {
  488. //                                                                printf("err\n");
  489. //                                                        }
  490. //                                                        offset += len;
  491. //                                                        isIdr = true;
  492. //                                                }
  493. //                                                break;
  494. //                                                case 6: // SEI
  495. //                                                {
  496. //                                                        int a = 5;
  497. //                                                }
  498. //                                                break;
  499. //                                                case 7: // SPS
  500. //                                                {
  501. //                                                        if (addStream)
  502. //                                                        {
  503. //                                                                videoId = MP4AddH264VideoTrack
  504. //                                                                (pHandle,
  505. //                                                                        timeScale,              // һ   Ӷ   timescale
  506. //                                                                        timeScale / frameRate,    // ÿ  ֡ ж  ٸ timescale
  507. //                                                                        width,                  // width
  508. //                                                                        height,                 // height
  509. //                                                                        pNalu[1],               // sps[1] AVCProfileIndication
  510. //                                                                        pNalu[2],               // sps[2] profile_compat
  511. //                                                                        pNalu[3],               // sps[3] AVCLevelIndication
  512. //                                                                        3);                     // 4 bytes length before each NAL unit
  513. //                                                                if (videoId == MP4_INVALID_TRACK_ID)
  514. //                                                                {
  515. //                                                                        printf("Error:Can't add track.\n");
  516. //                                                                        return -1;
  517. //                                                                }
  518. //                                                                MP4SetVideoProfileLevel(pHandle, 0x7F);
  519. //                                                                addStream = 0;
  520. //                                                                len = getNextNalu(pBuf, e->Frame.DataLength, offset);
  521. //                                                                MP4AddH264SequenceParameterSet(pHandle, videoId, pNalu, len);
  522. //                                                                goto gotNal;
  523. //                                                        }
  524. //
  525. //
  526. //                                                }
  527. //                                                break;
  528. //                                                case 8: // PPS
  529. //                                                {
  530. //                                                        len = getNextNalu(pBuf, e->Frame.DataLength, offset);
  531. //                                                        printf("pps(%d)\n", len);
  532. //                                                        MP4AddH264PictureParameterSet(pHandle, videoId, pNalu, len);
  533. //
  534. //                                                        goto gotNal;
  535. //                                                }
  536. //                                                break;
  537. //                                                case 9: // ֽ  
  538. //                                                        break;
  539. //                                                case 10: //   н     
  540. //                                                        break;
  541. //                                                default:
  542. //
  543. //                                                        break;
  544. //                                                }
  545. //                                        }
  546. //                                };
  547. //                        }
  548. //                        auto buffer = he->GetNV12Buffer();
  549. //                        buffer.Timestamp = GetClock();
  550. //                        for (int i = 0; i < height; i++)
  551. //                        {
  552. //                                memcpy(buffer.GetY() + i * buffer.GetPitch(), buf + i * width, width);
  553. //                        }
  554. //                        for (int i = 0; i < height / 2; i++)
  555. //                        {
  556. //                                memcpy(buffer.GetUV() + i * buffer.GetPitch(), buf + width * height + i * width, width);
  557. //                        }
  558. //                        buffer.IsForceIdr = false;
  559. //                        he->Write(buffer);
  560. //                }
  561. //                catch (const std::exception& e) {
  562. //                        printf(e.what());
  563. //                        printf("\n");
  564. //                }
  565. //                catch (...) {
  566. //                        printf("    δ֪ 쳣\n");
  567. //                }
  568. //        }
  569. //        virtual void OnCaptrue(const unsigned char* buf, int len, int width, int height, AC::CodingFormat format, unsigned int timestamp) override
  570. //        {
  571. //
  572. //        }
  573. //};
  574. //
  575. //
  576. //
  577. //
  578. ////   ڹ   
  579. //static        LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  580. //{
  581. //
  582. //        switch (msg)
  583. //        {
  584. //        case WM_DESTROY:
  585. //                PostQuitMessage(0);
  586. //                break;
  587. //        }
  588. //
  589. //        return DefWindowProc(hWnd, msg, wParam, lParam);
  590. //}
  591. //void GdiScreenCaptureTest() {
  592. //        WNDCLASS wndclass;
  593. //        wndclass.style = CS_HREDRAW | CS_VREDRAW;
  594. //        wndclass.lpfnWndProc = WndProc;
  595. //        wndclass.cbClsExtra = 0;
  596. //        wndclass.cbWndExtra = 0;
  597. //        wndclass.hInstance = GetModuleHandle(NULL);
  598. //        wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  599. //        wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  600. //        wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
  601. //        wndclass.lpszMenuName = 0;
  602. //        wndclass.lpszClassName = L"DemoWindow";
  603. //        if (!RegisterClass(&wndclass))
  604. //        {
  605. //                MessageBox(NULL, TEXT("        ʧ  !"), L"", MB_ICONERROR);
  606. //        }
  607. //        auto title = L"       ";
  608. //        HWND        hwnd = CreateWindow(
  609. //                L"DemoWindow",
  610. //                title,
  611. //                WS_OVERLAPPEDWINDOW,
  612. //                0,
  613. //                0,
  614. //                1280,
  615. //                720,
  616. //                NULL,
  617. //                NULL,
  618. //                GetModuleHandle(NULL),
  619. //                NULL
  620. //        );
  621. //        ShowWindow(hwnd, SW_SHOW);
  622. //        sr.SetWindow(hwnd);
  623. //        sr.SetFormat(AC::PIXELFORMAT_NV12);
  624. //        sr.Initial();
  625. //        AC::GdiScreenCapture gc(NULL, AC::PIXELFORMAT_NV12);
  626. //        A a;
  627. //        gc.SetCallback(&a);
  628. //        gc.SetFramerate(20);
  629. //
  630. //
  631. //        AC::SoundCollector sc;
  632. //        AC::FdkaacEncoder fe({ 2,44100,16 });
  633. //        sc.SetBufferSize(1024);
  634. //        fe.SetAudioObjectType(AC::AudioObjectType::AUDIOOBJECTTYPE_AAC_LC);
  635. //        fe.SetBitrate(256 * 1024);
  636. //        fe.SetChannelMode(AC::ChannelMode::CHANNELMODE_2);
  637. //        fe.SetChannelOrder(AC::CHANNELORDER_WAV);
  638. //        fe.SetBitrateMode(AC::BitrateModde::BITRATEMODDE_CBR);
  639. //        fe.SetTransportType(AC::TRANSPORTTYPE_MP4_RAW);
  640. //
  641. //        if (!pHandle)
  642. //        {
  643. //                pHandle = MP4Create("test.mp4", 0);
  644. //                if (pHandle == MP4_INVALID_FILE_HANDLE)
  645. //                {
  646. //                        printf("ERROR:Create mp4 handle fialed.\n");
  647. //                        return;
  648. //                }
  649. //                MP4SetTimeScale(pHandle, 90000);
  650. //        }
  651. //
  652. //        //     AAC track
  653. //        auto audio = MP4AddAudioTrack(pHandle, 44100, 1024, MP4_MPEG4_AUDIO_TYPE);
  654. //        if (audio == MP4_INVALID_TRACK_ID)
  655. //        {
  656. //                printf("Add audio track failed!\n");
  657. //                return;
  658. //        }
  659. //        FILE* faac = NULL;
  660. //        faac = fopen("test.aac", "wb+");
  661. //        //       Ƶ   
  662. //        MP4SetAudioProfileLevel(pHandle, 0x02);
  663. //        auto si = fe.GetDecoderSpecificInfo();
  664. //        auto pSi = (uint8_t*)&si;
  665. //        uint8_t temp = pSi[0];
  666. //        pSi[0] = pSi[1];
  667. //        pSi[1] = temp;
  668. //        MP4SetTrackESConfiguration(pHandle, audio, (uint8_t*)&si, 2);
  669. //
  670. //        int64_t _atimestamp = 0;
  671. //        int64_t _aduration = 0;
  672. //
  673. //        fe.EncodedDataArrived += [&](auto s, auto e) {
  674. //                fwrite(e->Frame.Data, 1, e->Frame.DataLength, faac);
  675. //                MP4WriteSample(pHandle, audio, e->Frame.Data, e->Frame.DataLength, MP4_INVALID_DURATION/* _aduration * 44100 / 1000*/, 0, 1);
  676. //        };
  677. //        bool isRun = false;
  678. //        sc.DataArrived += [&](auto s, auto e) {
  679. //
  680. //
  681. //                atime += e->DataLength * 1000.0 / (44100 * 16 / 8 * 2);
  682. //
  683. //                if (_atimestamp)
  684. //                        _aduration = clock() - _atimestamp;
  685. //                /*_atimestamp = clock();*/
  686. //
  687. //                if (!isRun)
  688. //                {
  689. //                        _timestamp = GetClock();
  690. //                        /*atime = 0;
  691. //                        gc.Start();*/
  692. //                        isRun = true;
  693. //                }
  694. //
  695. //                fe.Write(e->Data, e->DataLength);
  696. //        };
  697. //        _timestamp = GetClock();
  698. //        sc.Start(2, 44100, 16);
  699. //        gc.Start();
  700. //
  701. //
  702. //        MSG msg;
  703. //        HACCEL hAccelTable = NULL;
  704. //        //     Ϣѭ  :
  705. //        while (GetMessage(&msg, NULL, 0, 0))
  706. //        {
  707. //                if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
  708. //                {
  709. //                        TranslateMessage(&msg);
  710. //                        DispatchMessage(&msg);
  711. //                }
  712. //        }
  713. //        gc.Stop();
  714. //        sc.Stop();
  715. //        MP4Close(pHandle, 0);
  716. //        fclose(faac);
  717. //}
  718. //
  719. //
  720. //
  721. //
  722. //int main(int argc, char* argv[])
  723. //{
  724. //        GdiScreenCaptureTest();
  725. //
  726. //        return 0;
  727. //}









  728. int main2(int argc, char** argv) {




  729.         ////std::vector<std::string> files;
  730.         ////FileHelper::GetFolderFiles("E:\\Download\\Incubus\\IncubusQuest ver10", files);
  731.         ////for (auto i : files)
  732.         ////{
  733.         ////        auto ws = AC::StringHelper::ShifJisToUnicode(i);
  734.         ////        auto  s = AC::StringHelper::UnicodeToAnsi(ws);
  735.         ////        int ret = rename(i.c_str(), s.c_str());
  736.         ////}


  737.         //int d;
  738.         //scanf("%d", &d);

  739.         //auto ox = AC::StringHelper::Sprintf("%d", 15);

  740.         ////AC::SoundCollectorTest();
  741.         //AC::SoundPlayTest();
  742.         //AC::WavFileReader read;
  743.         //AC::SoundPlay sp;
  744.         //unsigned char buf[1024];
  745.         //if (read.OpenWavFile("test_music.wav"))
  746.         //{
  747.         //        int channels, sampleRate, bitsPerSample;
  748.         //        channels = read.GetChannels();
  749.         //        sampleRate = read.GetSampleRate();
  750.         //        bitsPerSample = read.GetBitsPerSample();
  751.         //        sp.Opened += [&](auto s, auto e) {
  752.         //                printf("  ʼ    :Channels %d SampleRate %d BitsPerSample %d\n", e->Format.Channels, e->Format.SampleRate, e->Format.BitsPerSample);
  753.         //        };
  754.         //        sp.DataDone += [&](auto s, auto e) {
  755.         //                printf("%p   ݲ      :    %d\n", e->Data, e->DataLength);
  756.         //        };
  757.         //        sp.Closed += [&](auto s, auto e) {
  758.         //                printf(" رղ   \n");
  759.         //        };
  760.         //        sp.Error += [&](auto s, auto e) {
  761.         //                printf("%s\n", e->Message.c_str());
  762.         //        };
  763.         //        sp.Open(read.GetChannels(), read.GetSampleRate(), read.GetBitsPerSample());
  764.         //        int size;
  765.         //        do
  766.         //        {
  767.         //                //  ȡ  Ƶ   
  768.         //                size = read.ReadData(buf, 1024);
  769.         //                if (size > 0)
  770.         //                {
  771.         //                        sp.Write(buf, size);
  772.         //                }
  773.         //        } while (size);
  774.         //}

  775.         ////auto _play = acf_play_create();
  776.         //////acf_play_set_beginCallback(_play, beginCallback);
  777.         //////acf_play_set_renderCallback(_play, renderCallback);
  778.         ////acf_play_play(_play, "212144271-1-192.mp4", 0);

  779.         return 0;
  780. }




复制代码





上一篇:模糊等价矩阵的求法(Matlab)
下一篇:PHP DES加密解密
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|php中文网 | cnphp.com ( 赣ICP备2021002321号-2 )51LA统计

GMT+8, 2024-5-3 08:56 , Processed in 0.179264 second(s), 37 queries , Gzip On.

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2020, Tencent Cloud.

申明:本站所有资源皆搜集自网络,相关版权归版权持有人所有,如有侵权,请电邮(fiorkn@foxmail.com)告之,本站会尽快删除。

快速回复 返回顶部 返回列表