File: videowmark-win.cc

package info (click to toggle)
audiowmark 0.6.5-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,168 kB
  • sloc: cpp: 10,571; sh: 1,867; makefile: 107; python: 34
file content (857 lines) | stat: -rw-r--r-- 24,531 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
/*
 * Copyright (C) Andreas Strohmeier
 *
 * This program is a port of the Linux bash script videowmark,
 * which is part of the audiowmark program by Stefan Westerfeld,
 * into the C++ programming language.
 * To keep it simple, there is only this single CPP file without a
 * header file.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

 //---------------------------------------------------------------------------

#include <iostream>
#include <string>
#include <cstdio>
#include <windows.h>
#include <algorithm>
#include <filesystem>

//---------------------------------------------------------------------------

using namespace std;

//---------------------------------------------------------------------------

string g_sVersion = "videowmark 0.0.5";
string g_sFFMPEG_VERBOSE = "-v error";
string g_sFFProbe = "ffprobe.exe";
string g_sFFMpeg = "ffmpeg.exe";
string g_sAudiowmark = "audiowmark.exe";
int g_iQuiet = 0;

//---------------------------------------------------------------------------

#define STRING_LENGTH 4096

//---------------------------------------------------------------------------

//show message and exit
int die(string sErrorMsg)
{
    printf("videowmark: error: %s\n", sErrorMsg.c_str());
    exit(1);
}
//---------------------------------------------------------------------------

// Fix me
// Maybe there is es better solution
//
// The result of getenv is not correct in cygwin environment.
//
// Example: getenv("TEMP");
//
// Result : /cygdrive/c/path/to/temp
// But should be: c:\path\to\temp
//
// This is a workaround to repair the path
string repair_cygwin_path(string sPath)
{
int i = 0;
string sResult = "";

    if(int(sPath.find("/cygdrive/")) == 0 )
    {
        sPath.erase(0, 10);

        for(i = 0; i < int(sPath.length()); i++)
        {
            if(sPath[i] == '/')
            {
                sPath[i] = '\\';
            }
        }

        if(sPath[1] != ':')
        {
            sPath.insert(1, ":");
        }
    }

    sResult = sPath;

    return sResult;
}
//---------------------------------------------------------------------------

// Converts the Windows path to the UNIX path
string get_unix_path(string sPath)
{
int i = 0;
string sResult = "";

    for(i = 0; i < int(sPath.length()); i++)
    {
        if(sPath[i] == '\\')
        {
            sPath[i] = '/';
        }
    }

    sResult = sPath;

    return sResult;
}
//---------------------------------------------------------------------------

// Converts the UNIX path to the Windows path
string get_windows_path(string sPath)
{
int i = 0;
string sResult = "";

    for(i = 0; i < int(sPath.length()); i++)
    {
        if(sPath[i] == '/')
        {
            sPath[i] = '\\';
        }
    }

    sResult = sPath;

    return sResult;
}
//---------------------------------------------------------------------------

// Completes the path if necessary
string complete_path(string sPath)
{
int iPathLength = 0;
bool bUNIX = true;
string sResult = "";

    iPathLength = sPath.length();
    if(iPathLength > 0)
    {
        if(int(sPath.find("\\")) >= 0 )
        {
            bUNIX = false;
        }

        if(bUNIX == true)
        {   // UNIX
            if(sPath[iPathLength-1] != '/')
            {
                sPath += "/";
            }
        }
        else
        {   // Windows
            if(sPath[iPathLength-1] != '\\')
            {
                sPath += "\\";
            }
        }
    }

    sResult = sPath;

    return sResult;
}
//---------------------------------------------------------------------------

// Set the current working directory
string set_working_dir(string sDestExe)
{
string sResult = "";
string sWorkingDir = "";
string sPath = "";
string sTempUnixPath = "";
char cWorkingDir[STRING_LENGTH] = {};

    //If something goes wrong while getting the working dir
    sResult = sDestExe;

    //Get current application directory
	GetModuleFileNameA(NULL, cWorkingDir, STRING_LENGTH);
    sWorkingDir = cWorkingDir;

    if(sDestExe.length() > 0 && sWorkingDir.length() > 0)
    {
        //Repair the path if needed
        sWorkingDir = repair_cygwin_path(sWorkingDir);

        //Convert Windows path to UNIX path
        sWorkingDir = get_unix_path(sWorkingDir);

        //Fix me:
        //Create filesystem::path object ( works in Cygwin only with UNIX path )
        filesystem::path p(sWorkingDir);

        //Get file path
        sPath = p.parent_path();

        //Completes the path if necessary
        sPath = complete_path(sPath);

        //Convert UNIX path to Windows path
        sPath = get_windows_path(sPath);

        //Build the filename
        sResult = "\"" + sPath + sDestExe + "\"";
    }

    return sResult;
}
//---------------------------------------------------------------------------

// Create the temp file
string create_temp_file(string sFilename)
{
string sTempPath = "";
string sFilenameWoPath = "";
string sTempUnixFilename = "";
string sTempFilename = "";

    //Fix me:
    //The result of getenv is not correct in cygwin environment.
    //
    //Example: getenv("TEMP");
    //
    //Result : /cygdrive/c/path/to/temp
    //But should be: c:\path\to\temp
    //
    //Get environment variable TEMP
    sTempPath = getenv("TEMP");

    //Repair the path if needed
    sTempPath = repair_cygwin_path(sTempPath);

    //Completes the path if necessary
    sTempPath = complete_path(sTempPath);

    //Convert Windows path to UNIX path
    sTempUnixFilename = get_unix_path(sFilename);

    //Fix me:
    //Create filesystem::path object ( works in Cygwin only with UNIX path )
    filesystem::path p(sTempUnixFilename);

    //Get filename without path
    sFilenameWoPath = p.filename();

    //Create filename
    sTempFilename = sTempPath + sFilenameWoPath;

    //Create temp file at destination
    if(CopyFile(sFilename.c_str(), sTempFilename.c_str(), false) == false)
    {
        die("Could not create temp file");
    }

    //Return file path
    return sTempFilename;
}
//---------------------------------------------------------------------------

// Delete file
bool delete_temp_file(string sFilename)
{
    return DeleteFile(sFilename.c_str());
}
//---------------------------------------------------------------------------

// Execute a command and get the results.
string ExecCmd(string sCMD /* [in] command to execute */ )
{
int i = 0;
string strResult = "";
char cmd[STRING_LENGTH] = {};
HANDLE hPipeRead = 0;
HANDLE hPipeWrite = 0;

    strcpy(cmd, sCMD.c_str());

    SECURITY_ATTRIBUTES saAttr = {sizeof(SECURITY_ATTRIBUTES)};
    saAttr.bInheritHandle = TRUE; // Pipe handles are inherited by child process.
    saAttr.lpSecurityDescriptor = NULL;

    // Create a pipe to get results from child's stdout.
    if (!CreatePipe(&hPipeRead, &hPipeWrite, &saAttr, 0))
        return strResult;

    STARTUPINFOA si = {sizeof(STARTUPINFOA)};
    si.dwFlags     = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
    si.hStdOutput  = hPipeWrite;
    si.hStdError   = hPipeWrite;
    si.wShowWindow = SW_HIDE; // Prevents cmd window from flashing.
                              // Requires STARTF_USESHOWWINDOW in dwFlags.

    PROCESS_INFORMATION pi = { 0 };

    BOOL fSuccess = CreateProcessA(NULL, (LPSTR)cmd, NULL, NULL, TRUE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi);
    if (! fSuccess)
    {
        CloseHandle(hPipeWrite);
        CloseHandle(hPipeRead);
        return strResult;
    }

    bool bProcessEnded = false;
    for (; !bProcessEnded ;)
    {
        // Give some timeslice (50 ms), so we won't waste 100% CPU.
        bProcessEnded = WaitForSingleObject( pi.hProcess, 50) == WAIT_OBJECT_0;

        // Even if process exited - we continue reading, if
        // there is some data available over pipe.
        char buf[STRING_LENGTH] = {};
        DWORD dwRead = 0;
        DWORD dwAvail = 0;

        for (;;)
        {
            if (!PeekNamedPipe(hPipeRead, NULL, 0, NULL, &dwAvail, NULL))
                break;

            if (!dwAvail) // No data available, return
                break;

            if (!ReadFile(hPipeRead, buf, min( sizeof(buf) - 1, (long unsigned int )dwAvail ), &dwRead, NULL) || !dwRead)
                // Error, the child process might ended
                break;

            //Get results
            strResult += buf;

            //Clean up entire buffer to remove trash
            for(i = 0; i < STRING_LENGTH; i++){buf[i] = 0;}
        }
    }

    CloseHandle(hPipeWrite);
    CloseHandle(hPipeRead);
    CloseHandle(pi.hProcess);
    CloseHandle(pi.hThread);

    return strResult;
}
//---------------------------------------------------------------------------

//auto detect codec and bitrate from input stream, generate ffmpeg options for audio encoder
string audio_encode_options(string sFilename)
{
string sParam = "";
string sCMD = "";
string sResult = "";
string sValue = "";
string sCodecName = "";
string sProbeResult = "";
string sTempResult = "";
string sBitRate = "";
string sData = "";
int i = 0;
int iPos = 0;

    //ffprobe -v error -print_format compact -show_streams "$1"
    sParam = "-print_format compact -show_streams \"" + sFilename + "\"";
    sCMD = g_sFFProbe + " " + g_sFFMPEG_VERBOSE + " " + sParam;

    //Execute and get results
    sProbeResult = ExecCmd(sCMD);

    //Parse to find the audio line
    for(i = 0; i < int(sProbeResult.length()); i++)
    {
        if(sProbeResult[i] != 0x0d && sProbeResult[i] != 0x0a)
        {
            sTempResult += sProbeResult[i];
        }

        if(sProbeResult[i] == 0x0d)
        {
            if(int(sTempResult.find("codec_type=audio")) > 0)
            {
                sResult = sTempResult;
                break;
            }
            else
            {
                sTempResult = "";
            }
        }
    }

    //Parse to find codec_name and bit_rate
    if(sResult.length() > 0)
    {
        sValue = "codec_name=";
        iPos = int(sResult.find(sValue));
        if(iPos > 0)
        {
            iPos += sValue.length();
            while(sResult[iPos] != '|')
            {
                sCodecName += sResult[iPos];
                iPos++;
            }
        }

        sValue = "bit_rate=";
        iPos = int(sResult.find(sValue));
        if(iPos > 0)
        {
            iPos += sValue.length();
            while(sResult[iPos] != '|')
            {
                sBitRate += sResult[iPos];
                iPos++;
            }
        }

        // opus encoder is experimental, ffmpeg recommends libopus for encoding
        if (sCodecName == "opus")
        {
            sCodecName = "libopus";
        }

        sResult = "-c:a " + sCodecName;

        if (sBitRate != "N/A")
        {
            sResult += " -ab " + sBitRate;
        }
    }

    return sResult;
}
//---------------------------------------------------------------------------

// count number of audio and video streams, typical output: "audio=1:video=1"
string audio_video_stream_count(string sFilename)
{
string sParam = "";
string sCMD = "";
string sResult = "";
char cResult[STRING_LENGTH] = {};
int iVideoCodec = 0;
int iAudioCodec = 0;

    //ffprobe -v error -print_format compact -show_streams "$1"
    sParam = "-print_format compact -show_streams \"" + sFilename + "\"";
    sCMD = g_sFFProbe + " " + g_sFFMPEG_VERBOSE + " " + sParam;

    //Execute and get results
    sResult = ExecCmd(sCMD);

    if(sResult.length() > 0)
    {
        if(int(sResult.find("codec_type=audio")) > 0)
        {
            iAudioCodec = 1;
        }

        if(int(sResult.find("codec_type=video")) > 0)
        {
            iVideoCodec = 1;
        }

        sprintf(cResult, "audio=%d:video=%d", iAudioCodec, iVideoCodec);

        sResult = cResult;
    }

    return sResult;
}
//---------------------------------------------------------------------------

// get file extension
string extension(string sFilename)
{
string sResult = "";
string sTempUnixFilename = "";

    //Fix me:
    //filesystem::path in Cygwin only works with /
    sTempUnixFilename = get_unix_path(sFilename);

    filesystem::path p(sTempUnixFilename);

    sResult = p.extension();

    return sResult;
}
//---------------------------------------------------------------------------

// add watermark to file
void add_watermark(string sInFile, string sOutFile, string sHash, string sARGS)
{
string sCMD = "";
string sParam = "";
string sExtIn = "";
string sExtOut = "";
string sResult = "";
string sStreamCount = "";
string sEncodeOptions = "";
string sTempFilenameVideo = "";
string sTempFilenameAudio = "";
string sTempFilenameAudioWM = "";

    // check file extensions
    sExtIn = extension(sInFile);
    sExtOut = extension(sOutFile);
    if(sExtIn != sExtOut)
    {
        die("input/output extension must match ('" + sExtIn + "' vs. '" + sExtOut + "')");
    }

    // check audio/video stream count
    sStreamCount = audio_video_stream_count(sInFile);
    if(sStreamCount != "audio=1:video=1")
    {
        printf("videowmark: detected input file stream count: %s\n", sStreamCount.c_str());
        die ("input file must have one audio stream and one video stream");
    }

    // create tmpfiles
    sTempFilenameVideo = create_temp_file(sInFile);

    // create audio tmpfilename ( just the name, not the file )
    sTempFilenameAudio = sTempFilenameVideo + ".wav";

    // create wm_audio tmpfilename ( just the name, not the file )
    sTempFilenameAudioWM = sTempFilenameVideo + "_wm.wav";

    // get audio as wav
    //ffmpeg $FFMPEG_VERBOSE -y -i "$in_file" -f wav -rf64 always "$wav"
    sParam = "-y -i \"" + sTempFilenameVideo + "\" -f wav -rf64 always \"" + sTempFilenameAudio + "\"";
    sCMD = g_sFFMpeg + " " +  g_sFFMPEG_VERBOSE + " " + sParam;

    //Execute and get results
    sResult = ExecCmd(sCMD);

    //Show detailed FFMpeg results
    if(g_sFFMPEG_VERBOSE == "-v info"){printf("%s\n", sResult.c_str());}

    if(sResult.length() > 0)
    {
        if(int(sResult.find("Error")) > 0)
        {
            //Clean up
            delete_temp_file(sTempFilenameVideo);
            delete_temp_file(sTempFilenameAudio);
            delete_temp_file(sTempFilenameAudioWM);

            die("extracting audio from video failed (ffmpeg)");
        }
    }

    // add watermark
    // audiowmark add "${AUDIOWMARK_ARGS[@]}" "$orig_wav" "$wm_wav" "$bits" --set-input-label "$in_file" --set-output-label "$out_file" --output-format rf64"
    sEncodeOptions = audio_encode_options(sInFile);
    if(g_iQuiet == 0){printf("Audio Codec:  %s\n", sEncodeOptions.c_str());}
    sParam = "add " + sARGS + " \"" + sTempFilenameAudio + "\" \"" + sTempFilenameAudioWM + "\" " + sHash + " --set-input-label \"" + sInFile + "\" --set-output-label \"" + sOutFile + "\" --output-format rf64";
    sCMD = g_sAudiowmark + " " + sParam;

    //Execute and get results
    sResult = ExecCmd(sCMD);

    if(sResult.length() > 0)
    {
        if(int(sResult.find("Error")) > 0)
        {
            //Show detailed cause of error
            if(g_sFFMPEG_VERBOSE == "-v info"){printf("%s\n", sResult.c_str());}

            //Clean up
            delete_temp_file(sTempFilenameVideo);
            delete_temp_file(sTempFilenameAudio);
            delete_temp_file(sTempFilenameAudioWM);

            die("watermark generation failed (audiowmark)");
        }
    }

    //Show Audiowmark results
    if(g_iQuiet == 0)
    {
        printf("%s\n", sResult.c_str());
    }

    // rejoin
    // 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"
    sParam = "-y -i \"" + sTempFilenameVideo + "\" -i \"" + sTempFilenameAudioWM + "\" -c:v copy " + sEncodeOptions + " -map 0:v:0 -map 1:a:0 \"" + sOutFile + "\"";
    sCMD = g_sFFMpeg + " " +  g_sFFMPEG_VERBOSE + " " + sParam;

    //Execute and get results
    sResult = ExecCmd(sCMD);

    //Show detailed FFMpeg results
    if(g_sFFMPEG_VERBOSE == "-v info"){printf("%s\n", sResult.c_str());}

    if(sResult.length() > 0)
    {
        if(int(sResult.find("Error")) > 0)
        {
            //Clean up
            delete_temp_file(sTempFilenameVideo);
            delete_temp_file(sTempFilenameAudio);
            delete_temp_file(sTempFilenameAudioWM);

            die("merging video and watermarked audio failed (ffmpeg)");
        }
    }

    //Clean up
    delete_temp_file(sTempFilenameVideo);
    delete_temp_file(sTempFilenameAudio);
    delete_temp_file(sTempFilenameAudioWM);
}
//---------------------------------------------------------------------------

// get watermark from file
void get_watermark(string sFilename, string sARGS)
{
string sParam = "";
string sCMD = "";
string sTempPath = "";
string sResult = "";
string sTempFilenameVideo = "";
string sTempFilenameAudio = "";

    // check audio/video stream count
    sResult = audio_video_stream_count(sFilename);

    if(sResult.length() > 0)
    {
        if(sResult != "audio=1:video=1" )
        {
            printf("videowmark: detected input file stream count: %s\n", sResult.c_str());
            die("input file must have one audio stream and one video stream");
        }
    }

    // create video tmpfile
    sTempFilenameVideo = create_temp_file(sFilename);

    // create audio tmpfilename ( just the name, not the file )
    sTempFilenameAudio = sTempFilenameVideo + ".wav";

    // get audio as wav
    //ffmpeg $FFMPEG_VERBOSE -y -i "$in_file" -f wav -rf64 always "$wav" || die "extracting audio from video failed (ffmpeg)"
    sParam = "-y -i \"" + sTempFilenameVideo + "\" -f wav -rf64 always \"" + sTempFilenameAudio + "\"";
    sCMD = g_sFFMpeg + " " + g_sFFMPEG_VERBOSE + " " + sParam;

    //Execute and get results
    sResult = ExecCmd(sCMD);

    //Show detailed FFMpeg results
    if(g_sFFMPEG_VERBOSE == "-v info"){printf("%s\n", sResult.c_str());}

    if(sResult.length() > 0)
    {
        if(int(sResult.find("Error")) > 0)
        {
            //Clean up
            delete_temp_file(sTempFilenameVideo);
            delete_temp_file(sTempFilenameAudio);

            die("extracting audio from video failed (ffmpeg)");
        }
    }

    // get watermark
    //audiowmark get "${AUDIOWMARK_ARGS[@]}" "$wav" || die "retrieving watermark from audio failed (audiowmark)"
    sParam = "get " + sARGS + " \"" + sTempFilenameAudio + "\"";
    sCMD = g_sAudiowmark + " " + sParam;

    //Execute and get results
    sResult = ExecCmd(sCMD);

    if(sResult.length() > 0)
    {
        if(int(sResult.find("Error")) > 0)
        {
            //Show detailed cause of error
            if(g_sFFMPEG_VERBOSE == "-v info"){printf("%s\n", sResult.c_str());}

            //Clean up
            delete_temp_file(sTempFilenameVideo);
            delete_temp_file(sTempFilenameAudio);

            die("retrieving watermark from audio failed (audiowmark)");
        }
    }

    //Show Audiowmark results
    printf("%s\n", sResult.c_str());

    //Clean up
    delete_temp_file(sTempFilenameVideo);
    delete_temp_file(sTempFilenameAudio);
}
//---------------------------------------------------------------------------

void show_version_and_exit()
{
    printf("%s\n", g_sVersion.c_str());
    exit(0);
}
//---------------------------------------------------------------------------

void show_help_and_exit()
{
    printf(
           "usage: videowmark <command> [ <args>... ]\n"
           "\n"
           "Commands:\n"
           "  * create a watermarked video file with a message\n"
           "    videowmark add <input_video> <watermarked_video> <message_hex>\n"
           "\n"
           "  * retrieve message\n"
           "    videowmark get <watermarked_video>\n"
           "\n"
           "Global options:\n"
           "  --strength <s>            set watermark strength\n"
           "  --key <file>              load watermarking key from file\n"
           "  -q, --quiet               disable information messages\n"
           "  -v, --verbose             enable ffmpeg verbose output\n"
           "  --version                 show the current videowmark version\n"
           "  --detect-speed            detect and correct replay speed difference\n"
           "  --detect-speed-patient    slower, more accurate speed detection\n"
           );

    exit(0);
}
//---------------------------------------------------------------------------

int main(int argc , char *argv[])
{
string sResult = "";
string sAction = "";
string sKeyfile = "";
string sFilename = "";
string sInFile = "";
string sOutFile = "";
string sHash = "";
string sARGS = "";
int i = 0;

    // Set the the current working directory
    g_sFFProbe = set_working_dir(g_sFFProbe);
    g_sFFMpeg = set_working_dir(g_sFFMpeg);
    g_sAudiowmark = set_working_dir(g_sAudiowmark);

    if(argc > 1)
    {
        // Get all args
        for(i = 1; i < argc; i++)
        {
            if(string(argv[i]) == "-v" || string(argv[i]) == "--verbose")
            {
                g_sFFMPEG_VERBOSE = "-v info";
            }
            else if(string(argv[i]) == "--version")
            {
                show_version_and_exit();
            }
            else if(string(argv[i]) == "-q" || string(argv[i]) == "--quiet")
            {
                sARGS += " -q";
                g_iQuiet = 1;
            }
            else if(string(argv[i]) == "--detect-speed" || string(argv[i]) == "--detect-speed-patient")
            {
                sARGS += " " + string(argv[i]);
            }
            else if(string(argv[i]) == "-h" || string(argv[i]) == "--help")
            {
                show_help_and_exit();
            }
            else if(string(argv[i]) == "--key")
            {
                if(argc >= i+1 )
                {
                    sARGS += " " + string(argv[i]) + " " + string(argv[i+1]);
                    i++;
                }
                else
                {
                    die("videowmark: error parsing command line arguments (use videowmark -h)");
                }
            }
            else if(string(argv[i]) == "--strength")
            {
                if(argc >= i+1 )
                {
                    sARGS += " " + string(argv[i]) + " " + string(argv[i+1]);
                    i++;
                }
                else
                {
                    die("videowmark: error parsing command line arguments (use videowmark -h)");
                }
            }
            else if(string(argv[i]) == "add" || string(argv[i]) == "get" || string(argv[i]) == "probe")
            {
                sAction = string(argv[i]);
            }
            else if(sInFile.length() == 0 && string(argv[i]).length() > 0)
            {
                sInFile = string(argv[i]);
            }
            else if(sOutFile.length() == 0 && string(argv[i]).length() > 0)
            {
                sOutFile = string(argv[i]);
            }
            else if(sHash.length() == 0 && string(argv[i]).length() > 0)
            {
                sHash = string(argv[i]);
            }
            else
            {
                //
            }
        }

        // Get and execute action
        if(sAction == "add" && sInFile.length() > 0 && sOutFile.length() > 0 && sHash.length() > 0)
        {
            add_watermark(sInFile, sOutFile, sHash, sARGS);
        }
        else if(sAction == "get" && sInFile.length() > 0)
        {
            get_watermark(sInFile, sARGS);
        }
        else if(sAction == "probe" && sInFile.length() > 0)
        {
            printf("%s %s\n", sInFile.c_str(),audio_encode_options(sInFile).c_str());
        }
        else
        {
            printf("videowmark: error parsing command line arguments (use videowmark -h)\n");
        }
    }
    else
    {
        show_help_and_exit();
    }

    return 0;
}
//---------------------------------------------------------------------------