]> git.sesse.net Git - vlc/blob - modules/misc/playlist/m3u.c
5d00746448e91f034b7f403a890fe8e4bf3c2afc
[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_interface.h>
31 #include <vlc_playlist.h>
32 #include <vlc_input.h>
33 #include <vlc_meta.h>
34
35 #include <errno.h>                                                 /* ENOMEM */
36
37 /*****************************************************************************
38  * Local prototypes
39  *****************************************************************************/
40 int Export_M3U ( vlc_object_t * );
41
42 /*****************************************************************************
43  * Export_M3U: main export function
44  *****************************************************************************/
45 static void DoChildren( playlist_t *p_playlist, playlist_export_t *p_export,
46                         playlist_item_t *p_root )
47 {
48     int i, j;
49
50     /* Go through the playlist and add items */
51     for( i = 0; i< p_root->i_children ; i++)
52     {
53         playlist_item_t *p_current = p_root->pp_children[i];
54         assert( p_current );
55
56         if( p_current->i_flags & PLAYLIST_SAVE_FLAG )
57             continue;
58
59         if( p_current->i_children >= 0 )
60         {
61             DoChildren( p_playlist, p_export, p_current );
62             continue;
63         }
64
65         assert( p_current->p_input->psz_uri );
66
67         /* General info */
68         char *psz_name = input_item_GetName( p_current->p_input );
69         if( psz_name && strcmp( p_current->p_input->psz_uri, psz_name ) )
70         {
71             char *psz_artist = input_item_GetArtist( p_current->p_input );
72             if( psz_artist == NULL ) psz_artist = strdup( "" );
73             if( psz_artist && *psz_artist )
74             {
75                 /* write EXTINF with artist */
76                 fprintf( p_export->p_file, "#EXTINF:%i,%s - %s\n",
77                           (int)( p_current->p_input->i_duration/1000000 ),
78                           psz_artist,
79                           p_current->p_input->psz_name);
80             }
81             else
82             {
83                 /* write EXTINF without artist */
84                 fprintf( p_export->p_file, "#EXTINF:%i,%s\n",
85                          (int)( p_current->p_input->i_duration/1000000 ),
86                           p_current->p_input->psz_name);
87             }
88             free( psz_artist );
89         }
90         free( psz_name );
91
92         /* VLC specific options */
93         for( j = 0; j < p_current->p_input->i_options; j++ )
94         {
95             fprintf( p_export->p_file, "#EXTVLCOPT:%s\n",
96                      p_current->p_input->ppsz_options[j][0] == ':' ?
97                      p_current->p_input->ppsz_options[j] + 1 :
98                      p_current->p_input->ppsz_options[j] );
99         }
100
101         fprintf( p_export->p_file, "%s\n",
102                  p_current->p_input->psz_uri );
103     }
104 }
105
106 int Export_M3U( vlc_object_t *p_this )
107 {
108     playlist_t *p_playlist = (playlist_t*)p_this;
109     playlist_export_t *p_export = (playlist_export_t *)p_playlist->p_private;
110
111     msg_Dbg(p_playlist, "saving using M3U format");
112
113     /* Write header */
114     fprintf( p_export->p_file, "#EXTM3U\n" );
115
116     DoChildren( p_playlist, p_export, p_export->p_root );
117     return VLC_SUCCESS;
118 }