1212#include " json.h"
1313#include < boost/filesystem.hpp>
1414#include < boost/date_time/posix_time/posix_time.hpp>
15- #include < boost/date_time/posix_time/posix_time_io.hpp>
16- #include < locale>
15+ #include < boost/regex.hpp>
1716#include < openssl/md5.h>
18- #include < fstream>
1917#include < numeric>
18+ #include < sys/wait.h>
19+
2020
2121using namespace std ;
2222using namespace boost ::filesystem;
@@ -42,37 +42,85 @@ VideoInfo al::get_info(std::string filename) {
4242}
4343
4444
45+ int fork_resize_video (path input, path temp_filename, Size size) {
46+ int pid = fork ();
47+ if (pid != 0 ) {
48+ /* We're in the parent process, return the child's pid. */
49+ return pid;
50+ }
51+ /* Otherwise, we're in the child process, so let's exec curl. */
52+ execlp (" ffmpeg" , " ffmpeg" , " -loglevel" , " panic" , " -i" , input.string ().c_str (), " -s" , (to_string (size.width ) + " x" + to_string (size.height )).c_str (), " -an" , temp_filename.string ().c_str (), NULL );
53+
54+ exit (100 );
55+ }
56+
57+ void al::resize_video (path input, path output, Size size) {
58+ path temp_output = output.parent_path ().append (" temp" );
59+ path temp_filename = path (temp_output).append (output.filename ().string ());
60+ if (!exists (temp_output)) {
61+ create_directories (temp_output);
62+ } else {
63+ if (exists (temp_filename)) {
64+ remove (temp_filename);
65+ }
66+ }
4567
46- void al::resize_video (string file, string output, Size size) {
4768 auto if_exists = exists (output);
4869
4970 if (!if_exists) {
50- // Calculate hash string per frame.
51- std::cout << " Resizing video... 0%" << std::endl;
52- VideoCapture capture;
53- capture.open (file);
54-
55- VideoInfo info = get_info (capture);
71+ if (system (" which ffmpeg" ) == 0 ) {
72+ cout << " Resizing video..." << endl;
73+
74+ int cpid = fork_resize_video (input, temp_filename, size);
75+ if (cpid == -1 ) {
76+ /* Failed to fork */
77+ cerr << " Fork failed" << endl;
78+ throw ;
79+ }
5680
57- VideoWriter writer (output, CV_FOURCC (' H' , ' 2' , ' 6' , ' 4' ), info.fps , size);
81+ /* Optionally, wait for the child to exit and get
82+ the exit status. */
83+ int status;
84+ waitpid (cpid, &status, 0 );
85+ if (! WIFEXITED (status)) {
86+ cerr << " The child was killed or segfaulted or something\n " << endl;
87+ }
5888
59-
60- int percent = 0 ;
61- int count = 0 , total = info.frame_count ;
62- cv::Mat image;
63- while (capture.read (image)) {
64- if (count / double (total) * 100 > percent) {
65- percent++;
66- std::cout << " Resizing video... " << percent << " %" << std::endl;
89+ status = WEXITSTATUS (status);
90+
91+
92+ cout << " done." << endl;
93+
94+ } else {
95+ // Calculate hash string per frame.
96+ cout << " Resizing video... 0%" << endl;
97+ VideoCapture capture;
98+ capture.open (input.string ());
99+
100+ VideoInfo info = get_info (capture);
101+
102+ VideoWriter writer (temp_filename.string (), CV_FOURCC (' H' , ' 2' , ' 6' , ' 4' ), info.fps , size);
103+
104+
105+ int percent = 0 ;
106+ int count = 0 , total = info.frame_count ;
107+ cv::Mat image;
108+ while (capture.read (image)) {
109+ if (count / double (total) * 100 > percent) {
110+ percent++;
111+ std::cout << " Resizing video... " << percent << " %" << std::endl;
112+ }
113+ cv::resize (image, image, size);
114+ writer.write (image);
115+ image.release ();
116+ count++;
67117 }
68- cv::resize (image, image, size);
69- writer.write (image);
70- image.release ();
71- count++;
118+
119+ writer.release ();
120+ capture.release ();
72121 }
73-
74- writer.release ();
75- capture.release ();
122+
123+ system ((" mv " + temp_filename.string () + " " + output.string ()).c_str ());
76124 }
77125}
78126
@@ -91,19 +139,27 @@ bool al::get_frames(string file, FrameVector &frames) {
91139 return true ;
92140}
93141
94- bool al::get_hash_strings (string file, string type, HashVector &hash_strings, string hash_file) {
95-
142+ void al::get_hash_strings (path filename, string type, HashVector &hash_strings, path hash_file) {
143+ path temp_path = hash_file.parent_path ().append (" temp" );
144+ path temp_filename = path (temp_path).append (hash_file.filename ().string ());
145+
146+ if (!exists (temp_path)) {
147+ create_directories (temp_path);
148+ } else {
149+ if (exists (temp_filename)) {
150+ remove (temp_filename);
151+ }
152+ }
153+
96154 auto if_exists = exists (hash_file);
97155 VideoCapture capture;
98156 if (if_exists) {
99157 // Read video frames hash string from file.
100158 std::cout << " Restore " << type << " value from file..." << std::endl;
101159
102- std::ifstream input_file (hash_file);
160+ std::ifstream input_file (hash_file. string () );
103161 HashVector hashs;
104-
105- int count;
106- count = 0 ;
162+
107163 while (input_file) {
108164 string line;
109165 getline (input_file, line);
@@ -112,15 +168,13 @@ bool al::get_hash_strings(string file, string type, HashVector &hash_strings, st
112168 input_file.close ();
113169
114170 hash_strings = hashs;
115- return true ;
116171 } else {
117172 // Calculate hash string per frame.
118173 VideoCapture capture;
119- capture.open (file );
174+ capture.open (filename. string () );
120175 VideoInfo info = get_info (capture);
121176 HashVector hashs;
122-
123-
177+
124178 cout << " Calculating " << type << " value... 0%" << endl;
125179 int percent = 0 ;
126180 int count = 0 , total = info.frame_count ;
@@ -139,13 +193,13 @@ bool al::get_hash_strings(string file, string type, HashVector &hash_strings, st
139193 hash_strings = hashs;
140194
141195 cout << " Save " << type << " value into file..." << endl;
142- std::ofstream output_file (hash_file );
196+ std::ofstream output_file (temp_filename. string () );
143197 ostream_iterator<string> output_iterator (output_file, " \n " );
144198 std::copy (hashs.begin (), hashs.end (), output_iterator);
145199 output_file.close ();
146- return true ;
200+
201+ system ((" mv " + temp_filename.string () + " " + hash_file.string ()).c_str ());
147202 }
148- return false ;
149203}
150204
151205
0 commit comments