]> git.sesse.net Git - vlc/blob - modules/misc/playlist/m3u.c
[patch] unifying meta-information access, the 2nd by Daniel Stränger
[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_playlist->i_size ; i++)
56     {
57         if( (p_playlist->pp_items[i]->i_flags & PLAYLIST_SAVE_FLAG) == 0 )
58         {
59             continue;
60         }
61
62         /* General info */
63         if( p_playlist->pp_items[i]->input.psz_name &&
64              strcmp( p_playlist->pp_items[i]->input.psz_name,
65                     p_playlist->pp_items[i]->input.psz_uri ) )
66         {
67             char *psz_artist =
68                 vlc_input_item_GetInfo( &p_playlist->pp_items[i]->input,
69                                         _(VLC_META_INFO_CAT), _(VLC_META_ARTIST) );
70             if( psz_artist && *psz_artist )
71             {
72                 /* write EXTINF with artist */
73                 fprintf( p_export->p_file, "#EXTINF:%i,%s - %s\n",
74                          (int)(p_playlist->pp_items[i]->input.i_duration/1000000),
75                          psz_artist,
76                          p_playlist->pp_items[i]->input.psz_name );
77             }
78             else
79             {
80                 /* write EXTINF without artist */
81                 fprintf( p_export->p_file, "#EXTINF:%i,%s\n",
82                        (int)(p_playlist->pp_items[i]->input.i_duration/1000000),
83                          p_playlist->pp_items[i]->input.psz_name );
84             }
85             if( psz_artist )
86                 free( psz_artist );
87         }
88
89         /* VLC specific options */
90         for( j = 0; j < p_playlist->pp_items[i]->input.i_options; j++ )
91         {
92             fprintf( p_export->p_file, "#EXTVLCOPT:%s\n",
93                      p_playlist->pp_items[i]->input.ppsz_options[j][0] == ':' ?
94                      p_playlist->pp_items[i]->input.ppsz_options[j] + 1 :
95                      p_playlist->pp_items[i]->input.ppsz_options[j] );
96         }
97
98         fprintf( p_export->p_file, "%s\n",
99                  p_playlist->pp_items[i]->input.psz_uri );
100     }
101     return VLC_SUCCESS;
102 }