Pencil2D  ff90c0872e88be3bf81c548cd60f01983012ec49
Pencil2D is an animation software for both bitmap and vector graphics. It is free, multi-platform, and open source.
 All Classes Functions
win32.cpp
1 /*
2 
3 Pencil - Traditional Animation Software
4 Copyright (C) 2005-2007 Patrick Corrieri & Pascal Naidon
5 Copyright (C) 2009 Mj Mendoza IV
6 Copyright (C) 2012-2017 Matthew Chiawen Chang
7 
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; version 2 of the License.
11 
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16 
17 */
18 
19 #include <cstdint>
20 #include <QFile>
21 #include <QProcess>
22 #include <QDir>
23 #include <QString>
24 #include <QProgressDialog>
25 #include <QImageReader>
26 #include <QImageWriter>
27 
28 #include "object.h"
29 #include "editor.h"
30 #include "layersound.h"
31 
32 #define MIN(a,b) ((a)>(b)?(b):(a))
33 
34 
35 
36 
37 int16_t safeSum( int16_t a, int16_t b )
38 {
39  if ( ( ( int )a + ( int )b ) > 32767 )
40  return 32767;
41  if ( ( ( int )a + ( int )b ) < -32768 )
42  return -32768;
43  return a + b;
44 }
45 
46 void initialise()
47 {
48  //qDebug() << "Initialize win32: <nothing, for now>";
49 
50  // QImageReader capabilities
51  QList<QByteArray> formats = QImageReader::supportedImageFormats();
52  foreach( QString format, formats )
53  {
54  //qDebug() << "QImageReader capability: " << format;
55  }
56 
57  // QImageWriter capabilities
58  formats = QImageWriter::supportedImageFormats();
59  foreach( QString format, formats )
60  {
61  //qDebug() << "QImageWriter capability: " << format;
62  }
63 }
64 
65 
66 void Editor::importMovie( QString filePath, int fps )
67 {
68  int i;
69  QSettings settings( PENCIL2D, PENCIL2D );
70 
71  qDebug() << "-------IMPORT VIDEO------" << filePath;
72 
73  // --------- Import all the temporary frames ----------
74  QDir::temp().mkdir( "pencil" );
75  QString tempPath = QDir::temp().absolutePath() + "/pencil/";
76 
77  if ( QFile::exists( QDir::current().currentPath() + "/plugins/ffmpeg.exe" ) == true )
78  {
79  QProgressDialog progress( "Importing movie...", "Abort", 0, 100, NULL );
80  progress.setWindowModality( Qt::WindowModal );
81  progress.show();
82  progress.setValue( 10 );
83  QProcess ffmpeg;
84  qDebug() << "./plugins/ffmpeg.exe -i \"" << filePath << "\" -r " << QString::number( fps ) << " -f image2 \"" << tempPath << "tmp_import%4d.png\"";
85  ffmpeg.start( "./plugins/ffmpeg.exe -i \"" + filePath + "\" -r " + QString::number( fps ) + " -f image2 \"" + tempPath + "tmp_import%4d.png\"" );
86  progress.setValue( 20 );
87  if ( ffmpeg.waitForStarted() == true )
88  {
89  if ( ffmpeg.waitForFinished() == true )
90  {
91  qDebug() << "stdout: " + ffmpeg.readAllStandardOutput();
92  qDebug() << "stderr: " + ffmpeg.readAllStandardError();
93  }
94  else
95  {
96  qDebug() << "ERROR: FFmpeg did not finish executing.";
97  }
98  }
99  else
100  {
101  qDebug() << "ERROR: Could not execute FFmpeg.";
102  }
103  progress.setValue( 50 );
104  QDir dir1( tempPath );
105  int nFiles = dir1.entryList().count();
106  i = 1;
107  QString frameNumberString = QString::number( i );
108  while ( frameNumberString.length() < 4 ) frameNumberString.prepend( "0" );
109  while ( QFile::exists( tempPath + "tmp_import" + frameNumberString + ".png" ) )
110  {
111  progress.setValue( 50 + i * 50 / nFiles );
112  if ( i>1 ) scrubForward();
113  importImage( tempPath + "tmp_import" + frameNumberString + ".png" );
114  i++;
115  frameNumberString = QString::number( i );
116  while ( frameNumberString.length() < 4 ) frameNumberString.prepend( "0" );
117  }
118  progress.setValue( 100 );
119  // --------- Clean up temp directory ---------
120  QDir dir( tempPath );
121  QStringList filtername( "*.*" );
122  QStringList entries = dir.entryList( filtername, QDir::Files, QDir::Type );
123  for ( int i = 0; i < entries.size(); i++ )
124  dir.remove( entries[ i ] );
125  }
126  else
127  {
128  qDebug() << "Please place ffmpeg.exe in plugins directory";
129  }
130 }