]> git.sesse.net Git - vlc/blob - modules/misc/playlist/m3u.c
Merge back branch 0.8.6-playlist-vlm to trunk.
[vlc] / modules / misc / playlist / m3u.c
1 /*****************************************************************************
2  * m3u.c :  M3U playlist export module
3  *****************************************************************************
4  * Copyright (C) 2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28
29 #include <vlc/vlc.h>
30 #include <vlc/intf.h>
31 #include <vlc_meta.h>
32
33 #include <errno.h>                                                 /* ENOMEM */
34
35 /*****************************************************************************
36  * Local prototypes
37  *****************************************************************************/
38 int Export_M3U ( vlc_object_t * );
39
40 /*****************************************************************************
41  * Export_M3U: main export function
42  *****************************************************************************/
43 int Export_M3U( vlc_object_t *p_this )
44 {
45     playlist_t *p_playlist = (playlist_t*)p_this;
46     playlist_export_t *p_export = (playlist_export_t *)p_playlist->p_private;
47     int i, j;
48
49     msg_Dbg(p_playlist, "saving using M3U format");
50
51     /* Write header */
52     fprintf( p_export->p_file, "#EXTM3U\n" );
53
54     /* Go through the playlist and add items */
55     for( i = 0; i< p_export->p_root->i_children ; i++)
56     {
57         playlist_item_t *p_current = p_export->p_root->pp_children[i];
58         if( p_current->i_flags & PLAYLIST_SAVE_FLAG )
59             continue;
60
61         /* General info */
62         if( p_current->p_input->psz_name &&
63              strcmp( p_current->p_input->psz_uri,
64                      p_current->p_input->psz_name ) )
65         {
66             char *psz_artist = p_current->p_input->p_meta->psz_artist ?
67                                strdup( p_current->p_input->p_meta->psz_artist ):
68                                strdup( "" );
69             if( psz_artist && *psz_artist )
70             {
71                 /* write EXTINF with artist */
72                 fprintf( p_export->p_file, "#EXTINF:%i,%s - %s\n",
73                           (int)( p_current->p_input->i_duration/1000000 ),
74                           psz_artist,
75                           p_current->p_input->psz_name);
76             }
77             else
78             {
79                 /* write EXTINF without artist */
80                 fprintf( p_export->p_file, "#EXTINF:%i,%s\n",
81                          (int)( p_current->p_input->i_duration/1000000 ),
82                           p_current->p_input->psz_name);
83             }
84             if( psz_artist )
85                 free( psz_artist );
86         }
87
88         /* VLC specific options */
89         for( j = 0; j < p_current->p_input->i_options; j++ )
90         {
91             fprintf( p_export->p_file, "#EXTVLCOPT:%s\n",
92                      p_current->p_input->ppsz_options[j][0] == ':' ?
93                      p_current->p_input->ppsz_options[j] + 1 :
94                      p_current->p_input->ppsz_options[j] );
95         }
96
97         fprintf( p_export->p_file, "%s\n",
98                  p_current->p_input->psz_uri );
99     }
100     return VLC_SUCCESS;
101 }