Commits

Anonymous committed 77a82f6

Created a MovieInfo object, for users to "introspect" the information about a movie file, and do stuff appropriately, like durations, etc.

Comments (0)

Files changed (4)

examples/__movie_test.py

 # Without a surface argument, the ffmpeg-wrapper uses the sdl_overlay library. 
 #screen=pygame.display.set_mode((640, 368))
 
+info = movie.MovieInfo(filename)
+print 
+print info.width, info.height
+print info.filename
+print info.aspect_ratio
+print info.duration
+print info.audio_codec
+print info.video_codec
+
 try:
     #this is to test that the movie module tests filenames to make sure they exist
     m=movie.Movie("gsdsjgsdj")
 
     int wanted_video_stream=1;
     int wanted_audio_stream=1;
-    int wanted_subti_stream=-1;
+    
     /* if seeking requested, we execute it */
     if (movie->start_time != AV_NOPTS_VALUE)
     {
 }
 PyMovie;
 
+typedef struct PyMovieInfo
+{
+    PyObject_HEAD
+    /* General purpose members */
+    double duration; //check
+    int width;//check
+    int height;//check
+    int sample_rate;//check
+    int channels;//check
+    char *vid_codec;//check
+    char *aud_codec;//check
+    char *filename;//check
+    double aspect_ratio;//check
+} PyMovieInfo;
+    
+
+
 /*command definitions */
 typedef struct seekCommand
 {
 /* 		Command management */
 void registerCommands(PyMovie *movie);
 
+/* 		MovieInfo management*/
+int _info_init(PyObject *self, PyObject *args, PyObject *kwds);
+void _info_init_internal(PyMovieInfo *self, const char *filename);
+void _info_dealloc(PyMovieInfo *info);
+PyObject* _info_get_duration (PyMovieInfo *info, void *closure);
+PyObject* _info_get_aspect_ratio (PyMovieInfo *info, void *closure);
+PyObject* _info_get_width (PyMovieInfo *info, void *closure);
+PyObject* _info_get_height (PyMovieInfo *info, void *closure);
+PyObject* _info_get_sample_rate(PyMovieInfo *info, void *closure);
+PyObject* _info_get_channels(PyMovieInfo *info, void *closure);
+PyObject* _info_get_video_codec(PyMovieInfo *info, void *closure);
+PyObject* _info_get_audio_codec(PyMovieInfo *info, void *closure);
+PyObject* _info_get_filename(PyMovieInfo *info, void *closure);
+
+
 #if 0
 /*		Subtitle Management*/
 int subtitle_render(void *arg);
 #include "gmovie.h"
 
+
 void _movie_init_internal(PyMovie *self, const char *filename, SDL_Surface *surf)
 {
     Py_INCREF(self);
 static char _movie_doc[] =
     /*DOC*/    "ffmpeg wrapper module for pygame";
 
+
+
+void _info_init_internal(PyMovieInfo *self, const char *filename)
+{
+	/* filename checking... */
+    PyObject *path = PyImport_ImportModule("os.path");
+    Py_INCREF(path);
+    PyObject *dict = PyModule_GetDict(path);
+   	PyObject *key = Py_BuildValue("s", "exists");
+   	Py_INCREF(key);
+	PyObject *existsFunc  = PyDict_GetItem(dict, key);
+	PyObject *boolean = PyObject_CallFunction(existsFunc, "s", filename);
+	Py_INCREF(boolean);
+	if(boolean==Py_False)
+	{
+		Py_DECREF(boolean);
+		Py_DECREF(key);
+		Py_DECREF(path);
+		RAISE(PyExc_ValueError, "This filename does not exist.");
+		return;
+		//Py_RETURN_NONE;
+	}
+	Py_DECREF(boolean);
+	Py_DECREF(key);
+	Py_DECREF(path);    
+    	
+    self->filename = (char *)PyMem_Malloc((sizeof(char)*strlen(filename))+sizeof(char));
+	strncpy(self->filename, filename, strlen(filename)+1);
+    AVFormatContext *ic;
+    AVFormatParameters params, *ap = &params;
+    int ret, err;
+
+    memset(ap, 0, sizeof(*ap));
+    ap->width = 0;
+    ap->height= 0;
+    ap->time_base= (AVRational)
+                   {
+                       1, 25
+                   };
+    ap->pix_fmt = PIX_FMT_NONE;
+    
+    err = av_open_input_file(&ic, filename, NULL, 0, ap);
+    if (err < 0)
+    {
+        PyErr_Format(PyExc_IOError, "There was a problem opening up %s, due to %i", filename, err);  
+		ret = -1;
+        goto fail;
+    }
+    err = av_find_stream_info(ic);
+    if (err < 0)
+    {
+        PyErr_Format(PyExc_IOError, "%s: could not find codec parameters", filename);
+        ret = -1;
+        goto fail;
+    }
+    
+    if(ic->duration > 0)
+    {
+		double duration =(double)ic->duration;
+		duration /=AV_TIME_BASE;
+		//should now be number of seconds in a double
+		self->duration=duration;
+    }
+    else
+    {
+		self->duration = -1; //to indicate a negative duration value
+    }
+    int i;
+    for(i =0; i<ic->nb_streams; i++)
+    {
+		AVCodecContext *enc = ic->streams[i]->codec;
+		AVCodec *codec;
+		codec = avcodec_find_decoder(enc->codec_id);
+	
+        switch(enc->codec_type)
+        {
+        case CODEC_TYPE_AUDIO:
+            self->sample_rate=enc->sample_rate;
+	    	self->channels = enc->channels;
+	    	self->aud_codec = (char *)PyMem_Malloc((sizeof(char)*strlen(codec->name))+sizeof(char));
+	    	strncpy(self->aud_codec, codec->name, strlen(codec->name)+1);
+	    	break;
+        case CODEC_TYPE_VIDEO:
+            self->width = enc->width;
+	    	self->height = enc->height;
+	    	self->aspect_ratio = (double)self->width/(double)self->height;
+	    	self->vid_codec = (char *)PyMem_Malloc((sizeof(char)*strlen(codec->name))+sizeof(char));
+	    	strncpy(self->vid_codec, codec->name, strlen(codec->name)+1);
+            break;
+        
+        default:
+            break;
+        }
+    }
+
+    ret=0;
+fail:
+   //av_freep(ic);
+	return;
+	
+}
+
+int _info_init(PyObject *self, PyObject *args, PyObject *kwds)
+{
+	Py_INCREF(self);
+    const char *c;
+    
+    if (!PyArg_ParseTuple (args, "s", &c))
+    {
+        PyErr_SetString(PyExc_TypeError, "No valid arguments");
+        return -1;
+    }
+	
+    _info_init_internal((PyMovieInfo *)self, c);
+
+    PyObject *er;
+    er = PyErr_Occurred();
+    Py_XINCREF(er);
+    if(er)
+    {
+        PyErr_Print();
+    }
+    Py_XDECREF(er);
+    if(!self)
+    {
+        PyErr_SetString(PyExc_IOError, "No info object created.");
+        PyErr_Print();
+        Py_DECREF(self);
+        return -1;
+    }
+    Py_DECREF(self);
+    //PySys_WriteStdout("Returning from _movie_init\n");
+    return 0;
+}
+
+void _info_dealloc(PyMovieInfo *info)
+{
+    info->ob_type->tp_free((PyObject *) info);
+}
+
+PyObject* _info_get_duration (PyMovieInfo *info, void *closure)
+{
+    return PyFloat_FromDouble(info->duration);
+}
+
+
+PyObject* _info_get_aspect_ratio (PyMovieInfo *info, void *closure)
+{
+    return PyFloat_FromDouble(info->aspect_ratio);
+}
+
+PyObject* _info_get_width (PyMovieInfo *info, void *closure)
+{
+    return PyInt_FromLong((long)info->width);
+}
+
+PyObject* _info_get_height (PyMovieInfo *info, void *closure)
+{
+    return PyInt_FromLong((long)info->height);
+}
+
+PyObject* _info_get_sample_rate(PyMovieInfo *info, void *closure)
+{
+    return PyInt_FromLong((long)info->sample_rate);
+}
+
+PyObject* _info_get_channels(PyMovieInfo *info, void *closure)
+{
+    return PyInt_FromLong((long)info->channels);
+}
+
+PyObject* _info_get_video_codec(PyMovieInfo *info, void *closure)
+{
+    return PyString_FromString(info->vid_codec);
+}
+
+PyObject* _info_get_audio_codec(PyMovieInfo *info, void *closure)
+{
+    return PyString_FromString(info->aud_codec);
+}
+
+PyObject* _info_get_filename(PyMovieInfo *info, void *closure)
+{
+    return PyString_FromString(info->filename);
+}
+
+static PyGetSetDef _info_getsets[] =
+    {
+        { "duration",     (getter) _info_get_duration,     NULL,  NULL, NULL },
+        { "aspect_ratio", (getter) _info_get_aspect_ratio, NULL,  NULL, NULL },
+        { "sample_rate",  (getter) _info_get_sample_rate,  NULL,  NULL, NULL },
+        { "height",       (getter) _info_get_height,       NULL,  NULL, NULL },
+        { "width",        (getter) _info_get_width,        NULL,  NULL, NULL },
+        { "channels",     (getter) _info_get_channels,     NULL,  NULL, NULL },
+        { "video_codec",  (getter) _info_get_video_codec,  NULL,  NULL, NULL },
+        { "audio_codec",  (getter) _info_get_audio_codec,  NULL,  NULL, NULL },
+		{ "filename",     (getter) _info_get_filename,     NULL,  NULL, NULL },
+        { NULL,            NULL,                           NULL,  NULL, NULL }
+    };
+
+
+static PyTypeObject PyMovieInfo_Type =
+    {
+        PyObject_HEAD_INIT(NULL)
+        0,
+        "pygame._movie.MovieInfo",  /* tp_name */
+        sizeof (PyMovieInfo),       /* tp_basicsize */
+        0,                          /* tp_itemsize */
+        (destructor) _info_dealloc, /* tp_dealloc */
+        0,                          /* tp_print */
+        0,                          /* tp_getattr */
+        0,                          /* tp_setattr */
+        0,                          /* tp_compare */
+        0,                          /* tp_repr */
+        0,                          /* tp_as_number */
+        0,                          /* tp_as_sequence */
+        0,                          /* tp_as_mapping */
+        0,                          /* tp_hash */
+        0,                          /* tp_call */
+        0,                          /* tp_str */
+        0,                          /* tp_getattro */
+        0,                          /* tp_setattro */
+        0,                          /* tp_as_buffer */
+        Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
+        0,                          /* tp_doc */
+        0,                          /* tp_traverse */
+        0,                          /* tp_clear */
+        0,                          /* tp_richcompare */
+        0,                          /* tp_weaklistoffset */
+        0,                          /* tp_iter */
+        0,                          /* tp_iternext */
+        0,                          /* tp_methods */
+        0,                          /* tp_members */
+        _info_getsets,              /* tp_getset */
+        0,                          /* tp_base */
+        0,                          /* tp_dict */
+        0,                          /* tp_descr_get */
+        0,                          /* tp_descr_set */
+        0,                          /* tp_dictoffset */
+        (initproc) _info_init,      /* tp_init */
+        0,                          /* tp_alloc */
+        0,                          /* tp_new */
+        0,                          /* tp_free */
+        0,                          /* tp_is_gc */
+        0,                          /* tp_bases */
+        0,                          /* tp_mro */
+        0,                          /* tp_cache */
+        0,                          /* tp_subclasses */
+        0,                          /* tp_weaklist */
+        0                           /* tp_del */
+    };
+
+
 PyMODINIT_FUNC
 init_movie(void)
 {
     {
         MODINIT_ERROR;
     }
-    /*
-    PyAudioStream_Type.tp_new = PyType_GenericNew;
-    if (PyType_Ready(&PyAudioStream_Type) < 0) {
-       MODINIT_ERROR;
-}
-    PyVideoStream_Type.tp_new = PyType_GenericNew;
-    if (PyType_Ready(&PyVideoStream_Type) < 0) {
-       MODINIT_ERROR;
-}*/
+
+    PyMovieInfo_Type.tp_new = PyType_GenericNew;
+    if (PyType_Ready(&PyMovieInfo_Type) < 0)
+    {
+        MODINIT_ERROR;
+    }
+
     // Create the module
 
     module = Py_InitModule3 ("_movie", NULL, _movie_doc); //movie doc needed
     // Add the type to the module.
     Py_INCREF(&PyMovie_Type);
     PyModule_AddObject(module, "Movie", (PyObject*)&PyMovie_Type);
+
+    Py_INCREF(&PyMovieInfo_Type);
+    PyModule_AddObject(module, "MovieInfo", (PyObject *)&PyMovieInfo_Type);
 }