OSDN Git Service

import 0.9.3
[handbrake-jp/handbrake-jp.git] / qt4 / qhandbrake.cpp
1 #include "qhandbrake.h"
2
3 QHandBrake::QHandBrake( QObject *parent ) : QObject( parent )
4 {
5     hbHandle = hb_init_express( HB_DEBUG_NONE, 1 );
6     hbTitle = NULL;
7 }
8
9 QHandBrake::~QHandBrake()
10 {
11     hb_close( &hbHandle );
12 }
13
14 QString displayDuration( const QTime t )
15 {
16     return QString( "%1 %2, %3 %4, %5 %6").arg(t.hour()).arg(t.hour() == 1 ? "hour" : "hours").arg(t.minute()).arg(t.minute() == 1 ? "minute" : "minutes").arg(t.second()).arg(t.second() == 1 ? "second" : "seconds");
17 }
18
19 QStandardItemModel *QHandBrake::titleListModel()
20 {
21     hb_list_t *hbTitles;
22     int i, titleCount;
23
24     hbTitles = hb_get_titles( hbHandle );
25     titleCount = hb_list_count( hbTitles );
26     titleModel = new QStandardItemModel( titleCount, 2, this );
27
28     titleModel->setHorizontalHeaderItem( 0, new QStandardItem("Title") );
29     titleModel->setHorizontalHeaderItem( 1, new QStandardItem("Duration") );
30
31     for( i = 0; i < titleCount; ++i )
32     {
33         hb_title_t *t = ( hb_title_t * )hb_list_item( hbTitles, i );
34
35         QTime d = QTime( t->hours, t->minutes, t->seconds );
36         QString s = QString( "Title %1" ).arg( t->index );
37         QStandardItem *siTitle = new QStandardItem( s );
38         QStandardItem *siDuration = new QStandardItem( displayDuration( d ) );
39         titleModel->setItem( i, 0, siTitle );
40         titleModel->setItem( i, 1, siDuration );
41
42         siTitle->setCheckable( true );
43         siTitle->setEditable( false );
44         siTitle->setData( i, Qt::UserRole );
45
46         siDuration->setEditable( false );
47     }
48
49     return titleModel;
50 }
51
52 void QHandBrake::startScan( const QString sDevice )
53 {
54     hb_state_t s;
55     int titleCurrent = 0;
56     int titleCount = 0;
57
58     if( sDevice.isEmpty() )
59     {
60         qDebug("Passed an empty device/path");
61         return;
62     }
63
64     hb_scan( hbHandle, sDevice.toLocal8Bit(), 0 );
65
66     do {
67         hb_get_state( hbHandle, &s );
68         qApp->processEvents();
69         if( s.state == HB_STATE_SCANNING )
70         {
71             titleCurrent = s.param.scanning.title_cur;
72             titleCount = s.param.scanning.title_count;
73
74             if( titleCurrent > 0 )
75             {
76                 emit scanProgress( titleCurrent, titleCount );
77                 qApp->processEvents();
78             }
79         }
80     } while( s.state != HB_STATE_SCANDONE );
81
82     if( hb_list_count( hb_get_titles( hbHandle ) ) )
83     {
84         emit scanProgress( titleCount, titleCount );
85     }
86     else
87     {
88         qDebug("Scanning failed");
89         return;
90     }
91 }
92
93 void QHandBrake::encode()
94 {
95     if( hbHandle == NULL )
96     {
97         qDebug("Encode called too early");
98         return;
99     }
100
101     int i;
102     hb_list_t *titles = hb_get_titles( hbHandle );
103
104     for( i = 0; i < titleModel->rowCount(); ++i )
105     {
106         QStandardItem *si = titleModel->item( i, 0 );
107         if( si->checkState() == Qt::Checked )
108         {
109             hbTitle = ( hb_title_t * )hb_list_item( titles, si->data( Qt::UserRole ).toInt() );
110             hb_job_t *job = hbTitle->job;
111             // FIXME hardcoded params here! need to fix
112             job->pixel_ratio = 1;
113             job->vcodec = HB_VCODEC_FFMPEG;
114             job->vquality = -1.0;
115             job->vbitrate = 1600;
116             job->acodec = HB_ACODEC_LAME;
117             job->audios[0] = -1;
118             job->mux = HB_MUX_MP4;
119             job->subtitle = -1;
120             job->pass = 0;
121             job->file = strdup("/tmp/foo.mp4");
122             hb_add( hbHandle, job );
123         }
124     }
125
126     fprintf(stderr, "Calling hb_start...\n");
127     hb_start( hbHandle );
128 }