]> git.sesse.net Git - vlc/blob - modules/misc/playlist/m3u.c
5e69ae3f292a6e739e3ab3066de36f6c9f9274ba
[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 #include <vlc_charset.h>
37 #include <vlc_url.h>
38
39 #include <assert.h>
40
41 /*****************************************************************************
42  * Local prototypes
43  *****************************************************************************/
44 int Export_M3U ( vlc_object_t * );
45 int Export_M3U8( vlc_object_t * );
46
47 /*****************************************************************************
48  * Export_M3U: main export function
49  *****************************************************************************/
50 static void DoChildren( playlist_export_t *p_export, playlist_item_t *p_root,
51                         int (*pf_fprintf) (FILE *, const char *, ...) )
52 {
53     int i, j;
54
55     /* Go through the playlist and add items */
56     for( i = 0; i< p_root->i_children ; i++)
57     {
58         playlist_item_t *p_current = p_root->pp_children[i];
59         assert( p_current );
60
61         if( p_current->i_flags & PLAYLIST_SAVE_FLAG )
62             continue;
63
64         if( p_current->i_children >= 0 )
65         {
66             DoChildren( p_export, p_current, pf_fprintf );
67             continue;
68         }
69
70         /* General info */
71
72         char *psz_uri = input_item_GetURI( p_current->p_input );
73
74         assert( psz_uri );
75
76         char *psz_name = input_item_GetName( p_current->p_input );
77         if( psz_name && strcmp( psz_uri, psz_name ) )
78         {
79             char *psz_artist = input_item_GetArtist( p_current->p_input );
80             if( psz_artist == NULL ) psz_artist = strdup( "" );
81             mtime_t i_duration = input_item_GetDuration( p_current->p_input );
82             if( psz_artist && *psz_artist )
83             {
84                 /* write EXTINF with artist */
85                 pf_fprintf( p_export->p_file, "#EXTINF:%"PRIu64",%s - %s\n",
86                             i_duration / CLOCK_FREQ, psz_artist, psz_name);
87             }
88             else
89             {
90                 /* write EXTINF without artist */
91                 pf_fprintf( p_export->p_file, "#EXTINF:%"PRIu64",%s\n",
92                             i_duration / CLOCK_FREQ, psz_name);
93             }
94             free( psz_artist );
95         }
96         free( psz_name );
97
98         /* VLC specific options */
99         vlc_mutex_lock( &p_current->p_input->lock );
100         for( j = 0; j < p_current->p_input->i_options; j++ )
101         {
102             pf_fprintf( p_export->p_file, "#EXTVLCOPT:%s\n",
103                         p_current->p_input->ppsz_options[j][0] == ':' ?
104                         p_current->p_input->ppsz_options[j] + 1 :
105                         p_current->p_input->ppsz_options[j] );
106         }
107         vlc_mutex_unlock( &p_current->p_input->lock );
108
109         /* Stupid third party players don't understand file: URIs. */
110         char *psz_path = make_path( psz_uri );
111         if( psz_path != NULL )
112         {
113             free( psz_uri );
114             psz_uri = psz_path;
115         }
116         fprintf( p_export->p_file, "%s\n", psz_uri );
117         free( psz_uri );
118     }
119 }
120
121 int Export_M3U( vlc_object_t *p_this )
122 {
123     playlist_export_t *p_export = (playlist_export_t *)p_this;
124
125     msg_Dbg( p_export, "saving using M3U format");
126
127     /* Write header */
128     fputs( "#EXTM3U\n", p_export->p_file );
129
130     DoChildren( p_export, p_export->p_root, utf8_fprintf );
131     return VLC_SUCCESS;
132 }
133
134 int Export_M3U8( vlc_object_t *p_this )
135 {
136     playlist_export_t *p_export = (playlist_export_t *)p_this;
137
138     msg_Dbg( p_export, "saving using M3U8 format");
139
140     /* Write header */
141     fputs( "#EXTM3U\n", p_export->p_file );
142
143     DoChildren( p_export, p_export->p_root, fprintf );
144     return VLC_SUCCESS;
145 }