videowmark-win.cc 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  1. /*
  2. * Copyright (C) Andreas Strohmeier
  3. *
  4. * This program is a port of the Linux bash script videowmark,
  5. * which is part of the audiowmark program by Stefan Westerfeld,
  6. * into the C++ programming language.
  7. * To keep it simple, there is only this single CPP file without a
  8. * header file.
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. //---------------------------------------------------------------------------
  25. #include <iostream>
  26. #include <string>
  27. #include <cstdio>
  28. #include <windows.h>
  29. #include <algorithm>
  30. #include <filesystem>
  31. //---------------------------------------------------------------------------
  32. using namespace std;
  33. //---------------------------------------------------------------------------
  34. string g_sVersion = "videowmark 0.0.5";
  35. string g_sFFMPEG_VERBOSE = "-v error";
  36. string g_sFFProbe = "ffprobe.exe";
  37. string g_sFFMpeg = "ffmpeg.exe";
  38. string g_sAudiowmark = "audiowmark.exe";
  39. int g_iQuiet = 0;
  40. //---------------------------------------------------------------------------
  41. #define STRING_LENGTH 4096
  42. //---------------------------------------------------------------------------
  43. //show message and exit
  44. int die(string sErrorMsg)
  45. {
  46. printf("videowmark: error: %s\n", sErrorMsg.c_str());
  47. exit(1);
  48. }
  49. //---------------------------------------------------------------------------
  50. // Fix me
  51. // Maybe there is es better solution
  52. //
  53. // The result of getenv is not correct in cygwin environment.
  54. //
  55. // Example: getenv("TEMP");
  56. //
  57. // Result : /cygdrive/c/path/to/temp
  58. // But should be: c:\path\to\temp
  59. //
  60. // This is a workaround to repair the path
  61. string repair_cygwin_path(string sPath)
  62. {
  63. int i = 0;
  64. string sResult = "";
  65. if(int(sPath.find("/cygdrive/")) == 0 )
  66. {
  67. sPath.erase(0, 10);
  68. for(i = 0; i < int(sPath.length()); i++)
  69. {
  70. if(sPath[i] == '/')
  71. {
  72. sPath[i] = '\\';
  73. }
  74. }
  75. if(sPath[1] != ':')
  76. {
  77. sPath.insert(1, ":");
  78. }
  79. }
  80. sResult = sPath;
  81. return sResult;
  82. }
  83. //---------------------------------------------------------------------------
  84. // Converts the Windows path to the UNIX path
  85. string get_unix_path(string sPath)
  86. {
  87. int i = 0;
  88. string sResult = "";
  89. for(i = 0; i < int(sPath.length()); i++)
  90. {
  91. if(sPath[i] == '\\')
  92. {
  93. sPath[i] = '/';
  94. }
  95. }
  96. sResult = sPath;
  97. return sResult;
  98. }
  99. //---------------------------------------------------------------------------
  100. // Converts the UNIX path to the Windows path
  101. string get_windows_path(string sPath)
  102. {
  103. int i = 0;
  104. string sResult = "";
  105. for(i = 0; i < int(sPath.length()); i++)
  106. {
  107. if(sPath[i] == '/')
  108. {
  109. sPath[i] = '\\';
  110. }
  111. }
  112. sResult = sPath;
  113. return sResult;
  114. }
  115. //---------------------------------------------------------------------------
  116. // Completes the path if necessary
  117. string complete_path(string sPath)
  118. {
  119. int iPathLength = 0;
  120. bool bUNIX = true;
  121. string sResult = "";
  122. iPathLength = sPath.length();
  123. if(iPathLength > 0)
  124. {
  125. if(int(sPath.find("\\")) >= 0 )
  126. {
  127. bUNIX = false;
  128. }
  129. if(bUNIX == true)
  130. { // UNIX
  131. if(sPath[iPathLength-1] != '/')
  132. {
  133. sPath += "/";
  134. }
  135. }
  136. else
  137. { // Windows
  138. if(sPath[iPathLength-1] != '\\')
  139. {
  140. sPath += "\\";
  141. }
  142. }
  143. }
  144. sResult = sPath;
  145. return sResult;
  146. }
  147. //---------------------------------------------------------------------------
  148. // Set the current working directory
  149. string set_working_dir(string sDestExe)
  150. {
  151. string sResult = "";
  152. string sWorkingDir = "";
  153. string sPath = "";
  154. string sTempUnixPath = "";
  155. char cWorkingDir[STRING_LENGTH] = {};
  156. //If something goes wrong while getting the working dir
  157. sResult = sDestExe;
  158. //Get current application directory
  159. GetModuleFileNameA(NULL, cWorkingDir, STRING_LENGTH);
  160. sWorkingDir = cWorkingDir;
  161. if(sDestExe.length() > 0 && sWorkingDir.length() > 0)
  162. {
  163. //Repair the path if needed
  164. sWorkingDir = repair_cygwin_path(sWorkingDir);
  165. //Convert Windows path to UNIX path
  166. sWorkingDir = get_unix_path(sWorkingDir);
  167. //Fix me:
  168. //Create filesystem::path object ( works in Cygwin only with UNIX path )
  169. filesystem::path p(sWorkingDir);
  170. //Get file path
  171. sPath = p.parent_path();
  172. //Completes the path if necessary
  173. sPath = complete_path(sPath);
  174. //Convert UNIX path to Windows path
  175. sPath = get_windows_path(sPath);
  176. //Build the filename
  177. sResult = "\"" + sPath + sDestExe + "\"";
  178. }
  179. return sResult;
  180. }
  181. //---------------------------------------------------------------------------
  182. // Create the temp file
  183. string create_temp_file(string sFilename)
  184. {
  185. string sTempPath = "";
  186. string sFilenameWoPath = "";
  187. string sTempUnixFilename = "";
  188. string sTempFilename = "";
  189. //Fix me:
  190. //The result of getenv is not correct in cygwin environment.
  191. //
  192. //Example: getenv("TEMP");
  193. //
  194. //Result : /cygdrive/c/path/to/temp
  195. //But should be: c:\path\to\temp
  196. //
  197. //Get environment variable TEMP
  198. sTempPath = getenv("TEMP");
  199. //Repair the path if needed
  200. sTempPath = repair_cygwin_path(sTempPath);
  201. //Completes the path if necessary
  202. sTempPath = complete_path(sTempPath);
  203. //Convert Windows path to UNIX path
  204. sTempUnixFilename = get_unix_path(sFilename);
  205. //Fix me:
  206. //Create filesystem::path object ( works in Cygwin only with UNIX path )
  207. filesystem::path p(sTempUnixFilename);
  208. //Get filename without path
  209. sFilenameWoPath = p.filename();
  210. //Create filename
  211. sTempFilename = sTempPath + sFilenameWoPath;
  212. //Create temp file at destination
  213. if(CopyFile(sFilename.c_str(), sTempFilename.c_str(), false) == false)
  214. {
  215. die("Could not create temp file");
  216. }
  217. //Return file path
  218. return sTempFilename;
  219. }
  220. //---------------------------------------------------------------------------
  221. // Delete file
  222. bool delete_temp_file(string sFilename)
  223. {
  224. return DeleteFile(sFilename.c_str());
  225. }
  226. //---------------------------------------------------------------------------
  227. // Execute a command and get the results.
  228. string ExecCmd(string sCMD /* [in] command to execute */ )
  229. {
  230. int i = 0;
  231. string strResult = "";
  232. char cmd[STRING_LENGTH] = {};
  233. HANDLE hPipeRead = 0;
  234. HANDLE hPipeWrite = 0;
  235. strcpy(cmd, sCMD.c_str());
  236. SECURITY_ATTRIBUTES saAttr = {sizeof(SECURITY_ATTRIBUTES)};
  237. saAttr.bInheritHandle = TRUE; // Pipe handles are inherited by child process.
  238. saAttr.lpSecurityDescriptor = NULL;
  239. // Create a pipe to get results from child's stdout.
  240. if (!CreatePipe(&hPipeRead, &hPipeWrite, &saAttr, 0))
  241. return strResult;
  242. STARTUPINFOA si = {sizeof(STARTUPINFOA)};
  243. si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
  244. si.hStdOutput = hPipeWrite;
  245. si.hStdError = hPipeWrite;
  246. si.wShowWindow = SW_HIDE; // Prevents cmd window from flashing.
  247. // Requires STARTF_USESHOWWINDOW in dwFlags.
  248. PROCESS_INFORMATION pi = { 0 };
  249. BOOL fSuccess = CreateProcessA(NULL, (LPSTR)cmd, NULL, NULL, TRUE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi);
  250. if (! fSuccess)
  251. {
  252. CloseHandle(hPipeWrite);
  253. CloseHandle(hPipeRead);
  254. return strResult;
  255. }
  256. bool bProcessEnded = false;
  257. for (; !bProcessEnded ;)
  258. {
  259. // Give some timeslice (50 ms), so we won't waste 100% CPU.
  260. bProcessEnded = WaitForSingleObject( pi.hProcess, 50) == WAIT_OBJECT_0;
  261. // Even if process exited - we continue reading, if
  262. // there is some data available over pipe.
  263. char buf[STRING_LENGTH] = {};
  264. DWORD dwRead = 0;
  265. DWORD dwAvail = 0;
  266. for (;;)
  267. {
  268. if (!PeekNamedPipe(hPipeRead, NULL, 0, NULL, &dwAvail, NULL))
  269. break;
  270. if (!dwAvail) // No data available, return
  271. break;
  272. if (!ReadFile(hPipeRead, buf, min( sizeof(buf) - 1, (long unsigned int )dwAvail ), &dwRead, NULL) || !dwRead)
  273. // Error, the child process might ended
  274. break;
  275. //Get results
  276. strResult += buf;
  277. //Clean up entire buffer to remove trash
  278. for(i = 0; i < STRING_LENGTH; i++){buf[i] = 0;}
  279. }
  280. }
  281. CloseHandle(hPipeWrite);
  282. CloseHandle(hPipeRead);
  283. CloseHandle(pi.hProcess);
  284. CloseHandle(pi.hThread);
  285. return strResult;
  286. }
  287. //---------------------------------------------------------------------------
  288. //auto detect codec and bitrate from input stream, generate ffmpeg options for audio encoder
  289. string audio_encode_options(string sFilename)
  290. {
  291. string sParam = "";
  292. string sCMD = "";
  293. string sResult = "";
  294. string sValue = "";
  295. string sCodecName = "";
  296. string sProbeResult = "";
  297. string sTempResult = "";
  298. string sBitRate = "";
  299. string sData = "";
  300. int i = 0;
  301. int iPos = 0;
  302. //ffprobe -v error -print_format compact -show_streams "$1"
  303. sParam = "-print_format compact -show_streams \"" + sFilename + "\"";
  304. sCMD = g_sFFProbe + " " + g_sFFMPEG_VERBOSE + " " + sParam;
  305. //Execute and get results
  306. sProbeResult = ExecCmd(sCMD);
  307. //Parse to find the audio line
  308. for(i = 0; i < int(sProbeResult.length()); i++)
  309. {
  310. if(sProbeResult[i] != 0x0d && sProbeResult[i] != 0x0a)
  311. {
  312. sTempResult += sProbeResult[i];
  313. }
  314. if(sProbeResult[i] == 0x0d)
  315. {
  316. if(int(sTempResult.find("codec_type=audio")) > 0)
  317. {
  318. sResult = sTempResult;
  319. break;
  320. }
  321. else
  322. {
  323. sTempResult = "";
  324. }
  325. }
  326. }
  327. //Parse to find codec_name and bit_rate
  328. if(sResult.length() > 0)
  329. {
  330. sValue = "codec_name=";
  331. iPos = int(sResult.find(sValue));
  332. if(iPos > 0)
  333. {
  334. iPos += sValue.length();
  335. while(sResult[iPos] != '|')
  336. {
  337. sCodecName += sResult[iPos];
  338. iPos++;
  339. }
  340. }
  341. sValue = "bit_rate=";
  342. iPos = int(sResult.find(sValue));
  343. if(iPos > 0)
  344. {
  345. iPos += sValue.length();
  346. while(sResult[iPos] != '|')
  347. {
  348. sBitRate += sResult[iPos];
  349. iPos++;
  350. }
  351. }
  352. // opus encoder is experimental, ffmpeg recommends libopus for encoding
  353. if (sCodecName == "opus")
  354. {
  355. sCodecName = "libopus";
  356. }
  357. sResult = "-c:a " + sCodecName;
  358. if (sBitRate != "N/A")
  359. {
  360. sResult += " -ab " + sBitRate;
  361. }
  362. }
  363. return sResult;
  364. }
  365. //---------------------------------------------------------------------------
  366. // count number of audio and video streams, typical output: "audio=1:video=1"
  367. string audio_video_stream_count(string sFilename)
  368. {
  369. string sParam = "";
  370. string sCMD = "";
  371. string sResult = "";
  372. char cResult[STRING_LENGTH] = {};
  373. int iVideoCodec = 0;
  374. int iAudioCodec = 0;
  375. //ffprobe -v error -print_format compact -show_streams "$1"
  376. sParam = "-print_format compact -show_streams \"" + sFilename + "\"";
  377. sCMD = g_sFFProbe + " " + g_sFFMPEG_VERBOSE + " " + sParam;
  378. //Execute and get results
  379. sResult = ExecCmd(sCMD);
  380. if(sResult.length() > 0)
  381. {
  382. if(int(sResult.find("codec_type=audio")) > 0)
  383. {
  384. iAudioCodec = 1;
  385. }
  386. if(int(sResult.find("codec_type=video")) > 0)
  387. {
  388. iVideoCodec = 1;
  389. }
  390. sprintf(cResult, "audio=%d:video=%d", iAudioCodec, iVideoCodec);
  391. sResult = cResult;
  392. }
  393. return sResult;
  394. }
  395. //---------------------------------------------------------------------------
  396. // get file extension
  397. string extension(string sFilename)
  398. {
  399. string sResult = "";
  400. string sTempUnixFilename = "";
  401. //Fix me:
  402. //filesystem::path in Cygwin only works with /
  403. sTempUnixFilename = get_unix_path(sFilename);
  404. filesystem::path p(sTempUnixFilename);
  405. sResult = p.extension();
  406. return sResult;
  407. }
  408. //---------------------------------------------------------------------------
  409. // add watermark to file
  410. void add_watermark(string sInFile, string sOutFile, string sHash, string sARGS)
  411. {
  412. string sCMD = "";
  413. string sParam = "";
  414. string sExtIn = "";
  415. string sExtOut = "";
  416. string sResult = "";
  417. string sStreamCount = "";
  418. string sEncodeOptions = "";
  419. string sTempFilenameVideo = "";
  420. string sTempFilenameAudio = "";
  421. string sTempFilenameAudioWM = "";
  422. // check file extensions
  423. sExtIn = extension(sInFile);
  424. sExtOut = extension(sOutFile);
  425. if(sExtIn != sExtOut)
  426. {
  427. die("input/output extension must match ('" + sExtIn + "' vs. '" + sExtOut + "')");
  428. }
  429. // check audio/video stream count
  430. sStreamCount = audio_video_stream_count(sInFile);
  431. if(sStreamCount != "audio=1:video=1")
  432. {
  433. printf("videowmark: detected input file stream count: %s\n", sStreamCount.c_str());
  434. die ("input file must have one audio stream and one video stream");
  435. }
  436. // create tmpfiles
  437. sTempFilenameVideo = create_temp_file(sInFile);
  438. // create audio tmpfilename ( just the name, not the file )
  439. sTempFilenameAudio = sTempFilenameVideo + ".wav";
  440. // create wm_audio tmpfilename ( just the name, not the file )
  441. sTempFilenameAudioWM = sTempFilenameVideo + "_wm.wav";
  442. // get audio as wav
  443. //ffmpeg $FFMPEG_VERBOSE -y -i "$in_file" -f wav -rf64 always "$wav"
  444. sParam = "-y -i \"" + sTempFilenameVideo + "\" -f wav -rf64 always \"" + sTempFilenameAudio + "\"";
  445. sCMD = g_sFFMpeg + " " + g_sFFMPEG_VERBOSE + " " + sParam;
  446. //Execute and get results
  447. sResult = ExecCmd(sCMD);
  448. //Show detailed FFMpeg results
  449. if(g_sFFMPEG_VERBOSE == "-v info"){printf("%s\n", sResult.c_str());}
  450. if(sResult.length() > 0)
  451. {
  452. if(int(sResult.find("Error")) > 0)
  453. {
  454. //Clean up
  455. delete_temp_file(sTempFilenameVideo);
  456. delete_temp_file(sTempFilenameAudio);
  457. delete_temp_file(sTempFilenameAudioWM);
  458. die("extracting audio from video failed (ffmpeg)");
  459. }
  460. }
  461. // add watermark
  462. // audiowmark add "${AUDIOWMARK_ARGS[@]}" "$orig_wav" "$wm_wav" "$bits" --set-input-label "$in_file" --set-output-label "$out_file" --output-format rf64"
  463. sEncodeOptions = audio_encode_options(sInFile);
  464. if(g_iQuiet == 0){printf("Audio Codec: %s\n", sEncodeOptions.c_str());}
  465. sParam = "add " + sARGS + " \"" + sTempFilenameAudio + "\" \"" + sTempFilenameAudioWM + "\" " + sHash + " --set-input-label \"" + sInFile + "\" --set-output-label \"" + sOutFile + "\" --output-format rf64";
  466. sCMD = g_sAudiowmark + " " + sParam;
  467. //Execute and get results
  468. sResult = ExecCmd(sCMD);
  469. if(sResult.length() > 0)
  470. {
  471. if(int(sResult.find("Error")) > 0)
  472. {
  473. //Show detailed cause of error
  474. if(g_sFFMPEG_VERBOSE == "-v info"){printf("%s\n", sResult.c_str());}
  475. //Clean up
  476. delete_temp_file(sTempFilenameVideo);
  477. delete_temp_file(sTempFilenameAudio);
  478. delete_temp_file(sTempFilenameAudioWM);
  479. die("watermark generation failed (audiowmark)");
  480. }
  481. }
  482. //Show Audiowmark results
  483. if(g_iQuiet == 0)
  484. {
  485. printf("%s\n", sResult.c_str());
  486. }
  487. // rejoin
  488. // ffmpeg $FFMPEG_VERBOSE -y -i "$in_file" -i "$wm_wav" -c:v copy $(audio_encode_options "$in_file") -map 0:v:0 -map 1:a:0 "$out_file"
  489. sParam = "-y -i \"" + sTempFilenameVideo + "\" -i \"" + sTempFilenameAudioWM + "\" -c:v copy " + sEncodeOptions + " -map 0:v:0 -map 1:a:0 \"" + sOutFile + "\"";
  490. sCMD = g_sFFMpeg + " " + g_sFFMPEG_VERBOSE + " " + sParam;
  491. //Execute and get results
  492. sResult = ExecCmd(sCMD);
  493. //Show detailed FFMpeg results
  494. if(g_sFFMPEG_VERBOSE == "-v info"){printf("%s\n", sResult.c_str());}
  495. if(sResult.length() > 0)
  496. {
  497. if(int(sResult.find("Error")) > 0)
  498. {
  499. //Clean up
  500. delete_temp_file(sTempFilenameVideo);
  501. delete_temp_file(sTempFilenameAudio);
  502. delete_temp_file(sTempFilenameAudioWM);
  503. die("merging video and watermarked audio failed (ffmpeg)");
  504. }
  505. }
  506. //Clean up
  507. delete_temp_file(sTempFilenameVideo);
  508. delete_temp_file(sTempFilenameAudio);
  509. delete_temp_file(sTempFilenameAudioWM);
  510. }
  511. //---------------------------------------------------------------------------
  512. // get watermark from file
  513. void get_watermark(string sFilename, string sARGS)
  514. {
  515. string sParam = "";
  516. string sCMD = "";
  517. string sTempPath = "";
  518. string sResult = "";
  519. string sTempFilenameVideo = "";
  520. string sTempFilenameAudio = "";
  521. // check audio/video stream count
  522. sResult = audio_video_stream_count(sFilename);
  523. if(sResult.length() > 0)
  524. {
  525. if(sResult != "audio=1:video=1" )
  526. {
  527. printf("videowmark: detected input file stream count: %s\n", sResult.c_str());
  528. die("input file must have one audio stream and one video stream");
  529. }
  530. }
  531. // create video tmpfile
  532. sTempFilenameVideo = create_temp_file(sFilename);
  533. // create audio tmpfilename ( just the name, not the file )
  534. sTempFilenameAudio = sTempFilenameVideo + ".wav";
  535. // get audio as wav
  536. //ffmpeg $FFMPEG_VERBOSE -y -i "$in_file" -f wav -rf64 always "$wav" || die "extracting audio from video failed (ffmpeg)"
  537. sParam = "-y -i \"" + sTempFilenameVideo + "\" -f wav -rf64 always \"" + sTempFilenameAudio + "\"";
  538. sCMD = g_sFFMpeg + " " + g_sFFMPEG_VERBOSE + " " + sParam;
  539. //Execute and get results
  540. sResult = ExecCmd(sCMD);
  541. //Show detailed FFMpeg results
  542. if(g_sFFMPEG_VERBOSE == "-v info"){printf("%s\n", sResult.c_str());}
  543. if(sResult.length() > 0)
  544. {
  545. if(int(sResult.find("Error")) > 0)
  546. {
  547. //Clean up
  548. delete_temp_file(sTempFilenameVideo);
  549. delete_temp_file(sTempFilenameAudio);
  550. die("extracting audio from video failed (ffmpeg)");
  551. }
  552. }
  553. // get watermark
  554. //audiowmark get "${AUDIOWMARK_ARGS[@]}" "$wav" || die "retrieving watermark from audio failed (audiowmark)"
  555. sParam = "get " + sARGS + " \"" + sTempFilenameAudio + "\"";
  556. sCMD = g_sAudiowmark + " " + sParam;
  557. //Execute and get results
  558. sResult = ExecCmd(sCMD);
  559. if(sResult.length() > 0)
  560. {
  561. if(int(sResult.find("Error")) > 0)
  562. {
  563. //Show detailed cause of error
  564. if(g_sFFMPEG_VERBOSE == "-v info"){printf("%s\n", sResult.c_str());}
  565. //Clean up
  566. delete_temp_file(sTempFilenameVideo);
  567. delete_temp_file(sTempFilenameAudio);
  568. die("retrieving watermark from audio failed (audiowmark)");
  569. }
  570. }
  571. //Show Audiowmark results
  572. printf("%s\n", sResult.c_str());
  573. //Clean up
  574. delete_temp_file(sTempFilenameVideo);
  575. delete_temp_file(sTempFilenameAudio);
  576. }
  577. //---------------------------------------------------------------------------
  578. void show_version_and_exit()
  579. {
  580. printf("%s\n", g_sVersion.c_str());
  581. exit(0);
  582. }
  583. //---------------------------------------------------------------------------
  584. void show_help_and_exit()
  585. {
  586. printf(
  587. "usage: videowmark <command> [ <args>... ]\n"
  588. "\n"
  589. "Commands:\n"
  590. " * create a watermarked video file with a message\n"
  591. " videowmark add <input_video> <watermarked_video> <message_hex>\n"
  592. "\n"
  593. " * retrieve message\n"
  594. " videowmark get <watermarked_video>\n"
  595. "\n"
  596. "Global options:\n"
  597. " --strength <s> set watermark strength\n"
  598. " --key <file> load watermarking key from file\n"
  599. " -q, --quiet disable information messages\n"
  600. " -v, --verbose enable ffmpeg verbose output\n"
  601. " --version show the current videowmark version\n"
  602. " --detect-speed detect and correct replay speed difference\n"
  603. " --detect-speed-patient slower, more accurate speed detection\n"
  604. );
  605. exit(0);
  606. }
  607. //---------------------------------------------------------------------------
  608. int main(int argc , char *argv[])
  609. {
  610. string sResult = "";
  611. string sAction = "";
  612. string sKeyfile = "";
  613. string sFilename = "";
  614. string sInFile = "";
  615. string sOutFile = "";
  616. string sHash = "";
  617. string sARGS = "";
  618. int i = 0;
  619. // Set the the current working directory
  620. g_sFFProbe = set_working_dir(g_sFFProbe);
  621. g_sFFMpeg = set_working_dir(g_sFFMpeg);
  622. g_sAudiowmark = set_working_dir(g_sAudiowmark);
  623. if(argc > 1)
  624. {
  625. // Get all args
  626. for(i = 1; i < argc; i++)
  627. {
  628. if(string(argv[i]) == "-v" || string(argv[i]) == "--verbose")
  629. {
  630. g_sFFMPEG_VERBOSE = "-v info";
  631. }
  632. else if(string(argv[i]) == "--version")
  633. {
  634. show_version_and_exit();
  635. }
  636. else if(string(argv[i]) == "-q" || string(argv[i]) == "--quiet")
  637. {
  638. sARGS += " -q";
  639. g_iQuiet = 1;
  640. }
  641. else if(string(argv[i]) == "--detect-speed" || string(argv[i]) == "--detect-speed-patient")
  642. {
  643. sARGS += " " + string(argv[i]);
  644. }
  645. else if(string(argv[i]) == "-h" || string(argv[i]) == "--help")
  646. {
  647. show_help_and_exit();
  648. }
  649. else if(string(argv[i]) == "--key")
  650. {
  651. if(argc >= i+1 )
  652. {
  653. sARGS += " " + string(argv[i]) + " " + string(argv[i+1]);
  654. i++;
  655. }
  656. else
  657. {
  658. die("videowmark: error parsing command line arguments (use videowmark -h)");
  659. }
  660. }
  661. else if(string(argv[i]) == "--strength")
  662. {
  663. if(argc >= i+1 )
  664. {
  665. sARGS += " " + string(argv[i]) + " " + string(argv[i+1]);
  666. i++;
  667. }
  668. else
  669. {
  670. die("videowmark: error parsing command line arguments (use videowmark -h)");
  671. }
  672. }
  673. else if(string(argv[i]) == "add" || string(argv[i]) == "get" || string(argv[i]) == "probe")
  674. {
  675. sAction = string(argv[i]);
  676. }
  677. else if(sInFile.length() == 0 && string(argv[i]).length() > 0)
  678. {
  679. sInFile = string(argv[i]);
  680. }
  681. else if(sOutFile.length() == 0 && string(argv[i]).length() > 0)
  682. {
  683. sOutFile = string(argv[i]);
  684. }
  685. else if(sHash.length() == 0 && string(argv[i]).length() > 0)
  686. {
  687. sHash = string(argv[i]);
  688. }
  689. else
  690. {
  691. //
  692. }
  693. }
  694. // Get and execute action
  695. if(sAction == "add" && sInFile.length() > 0 && sOutFile.length() > 0 && sHash.length() > 0)
  696. {
  697. add_watermark(sInFile, sOutFile, sHash, sARGS);
  698. }
  699. else if(sAction == "get" && sInFile.length() > 0)
  700. {
  701. get_watermark(sInFile, sARGS);
  702. }
  703. else if(sAction == "probe" && sInFile.length() > 0)
  704. {
  705. printf("%s %s\n", sInFile.c_str(),audio_encode_options(sInFile).c_str());
  706. }
  707. else
  708. {
  709. printf("videowmark: error parsing command line arguments (use videowmark -h)\n");
  710. }
  711. }
  712. else
  713. {
  714. show_help_and_exit();
  715. }
  716. return 0;
  717. }
  718. //---------------------------------------------------------------------------