U
    $Pf                     @   sp   d dl mZ d dlZd dlZddlmZ ddlmZmZm	Z	m
Z
mZmZmZ G dd de
ZG d	d
 d
eZdS )    )unicode_literalsN   )FFmpegPostProcessor   )check_executableencodeArgumentencodeFilenamePostProcessingErrorprepend_extensionreplace_extensionshell_quotec                   @   s   e Zd ZdS )EmbedThumbnailPPErrorN)__name__
__module____qualname__ r   r   K/tmp/pip-unpacked-wheel-7zdooeg3/youtube_dl/postprocessor/embedthumbnail.pyr      s   r   c                       s&   e Zd Zd fdd	Zdd Z  ZS )EmbedThumbnailPPNFc                    s   t t| | || _d S )N)superr   __init___already_have_thumbnail)self
downloaderZalready_have_thumbnail	__class__r   r   r      s    zEmbedThumbnailPP.__init__c              
   C   s   |d }t |d}|ds0| jd g |fS |d d d }tjt|sd| jd g |fS dd	 }tj	|\}}|r|d
d  
 }|dkr||r| jd|  t|d}tt|t| |}d}|dkr\|dd}	tt|t|	 t|	d}
| jd|	  | |	|
ddg tt|	 t|d}tt|
t| |}|d dkrddddddddddg
}| jd|  | ||g|| | jstt| tt| tt|t| n8|d dkrtd d! d"D d }|d krtd#t|d$t|d$td%t|d$td&t|d$g}| jd'|  | jjd(d)rz| jd*t|  tj|tjtjd+}| \}}|jd,kr|d-d. }t|| jstt| d/|kr| jd0 n"tt| tt|t| ntd1g |fS )2NfilepathtempZ
thumbnailsz5[embedthumbnail] There aren't any thumbnails to embedfilenamez=Skipping embedding the thumbnail because the file is missing.c              	   S   sD   t t| d}|d}W 5 Q R X |dd dkoB|dd  dkS )Nrb   r      s   RIFF   s   WEBP)openr   read)pathfbr   r   r   is_webp-   s    z%EmbedThumbnailPP.run.<locals>.is_webpr   ZwebpzJ[ffmpeg] Correcting extension to webp and escaping path for thumbnail "%s")jpgZpng%#r)   z*[ffmpeg] Converting thumbnail "%s" to JPEGz-bsf:vZ
mjpeg2jpegextZmp3z-ccopyz-map01z-metadata:s:vztitle="Album cover"zcomment="Cover (Front)"z![ffmpeg] Adding thumbnail to "%s")Zm4aZmp4c                 s   s   | ]}t |d gr|V  qdS )z-vN)r   ).0xr   r   r   	<genexpr>\   s   z'EmbedThumbnailPP.run.<locals>.<genexpr>)ZAtomicParsleyatomicparsleyz,AtomicParsley was not found. Please install.Tz	--artworkz-oz([atomicparsley] Adding thumbnail to "%s"verboseFz&[debug] AtomicParsley command line: %s)stdoutstderrr   zutf-8replaces
   No changesz5The file format doesn't support embedding a thumbnailzCOnly mp3 and m4a/mp4 are supported for thumbnail embedding for now.)r
   getZ_downloaderZ	to_screenosr%   existsr   Zreport_warningsplitextlowerr   renamer7   Z
run_ffmpegremoveZrun_ffmpeg_multiple_filesr   nextr   r   paramsr   
subprocessPopenPIPEcommunicate
returncodedecodestrip)r   infor   Ztemp_filenameZthumbnail_filenamer(   _Zthumbnail_extZthumbnail_webp_filenameZescaped_thumbnail_filenameZescaped_thumbnail_jpg_filenameZthumbnail_jpg_filenameoptionsr3   cmdpr5   r6   msgr   r   r   run   s    





        

zEmbedThumbnailPP.run)NF)r   r   r   r   rN   __classcell__r   r   r   r   r      s   r   )
__future__r   r9   rA   Zffmpegr   utilsr   r   r   r	   r
   r   r   r   r   r   r   r   r   <module>   s   $