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