Spaces:
Sleeping
Sleeping
Update index.html
Browse files26 unmodified lines2728293031323334303132333435363738394041404142434445464347484546474849505152535455564950515253545556575859606162636465666768626970717273746869757677787980818283848586877172738889909192939475767778799596979882839910010110285103104105106878889901071081091101111121131141151161171181191201211221231241251261279495969798991001011021281291301311321331341351361371381391401061071411421431441451111461471131141481491501161171181191201211221511521531541551561271281291301311321331341571581591601611361371381391401411421431441621631641651661671681691481701711721731 unmodified line1751761771561781791801591811821831844 unmodified lines1891901911701921931941956 unmodified lines20220320418318418518618718818919019119219319420520620720820921021121221321421521621721821922022122222322419920020120220320420520620720820921021121221322522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329421729529629729826 unmodified lines
const TOOLS = [ { id: 'heic-jpg', title: 'HEIC → JPG', icon: 'image', color: 'bg-blue-500', description: 'iPhoneの写真を一般的な形式に変換' }, { id: 'm4a-mp3', title: 'M4A → MP3', icon: 'music', color: 'bg-purple-500', description: 'Apple録音データをMP3に変換' }, { id: 'mp4-mp3', title: '動画 → MP3', icon: 'volume-2', color: 'bg-orange-500', description: '動画から音声のみを抽出' }, { id: 'jpeg-pdf', title: '画像 → PDF', icon: 'file-text', color: 'bg-red-500', description: '複数の画像を1つのPDFに結合' }, { id: 'pdf-png', title: 'PDF → PNG', icon: 'files', color: 'bg-emerald-500', description: 'PDFの各ページを画像に変換' }, { id: 'youtube', title: 'YouTube 保存', icon: 'youtube', color: 'bg-rose-600', description: '動画・音声を最高画質で保存 (Colab経由)' }, { id: 'm4a-mp3', title: 'M4A → MP3', icon: 'music', color: 'bg-emerald-500', description: 'ボイスメモをMP3に変換' }, { id: 'mp4-mp3', title: 'MP4 → MP3', icon: 'mic', color: 'bg-purple-500', description: '動画から音声のみを抽出' }, { id: 'jpeg-pdf', title: 'JPEG → PDF', icon: 'file-text', color: 'bg-orange-500', description: '画像をPDFにまとめる' }, { id: 'pdf-png', title: 'PDF → PNG', icon: 'file-image', color: 'bg-red-500', description: 'PDFの各ページを画像に変換' }, { id: 'youtube', title: 'YouTube保存', icon: 'youtube', color: 'bg-rose-600', description: '動画をMP3またはMP4で取得' }, ];
function App() { const [selectedTool, setSelectedTool] = useState(null); const [files, setFiles] = useState([]); const [youtubeUrl, setYoutubeUrl] = useState(""); const [taskState, setTaskState] = useState({ status: 'idle', progress: 0, last_log: "" }); const [youtubeUrl, setYoutubeUrl] = useState(''); const [youtubeFormat, setYoutubeFormat] = useState('mp3'); // 保存形式ステート const [taskId, setTaskId] = useState(null); const [status, setStatus] = useState('idle'); // idle, processing, completed, failed const [progress, setProgress] = useState(0); const [lastLog, setLastLog] = useState(''); const [downloadUrl, setDownloadUrl] = useState(null); const [youtubeEnabled, setYoutubeEnabled] = useState(false); const [error, setError] = useState(null);
useEffect(() => { fetch('/api/config') .then(res => res.json()) .then(data => setYoutubeEnabled(data.youtube_enabled)) .catch(() => setYoutubeEnabled(false)); }, []);
const startProcessing = async () => { if (!selectedTool) return; setTaskState({ status: 'uploading', progress: 10, last_log: "ファイルをアップロード中..." }); setDownloadUrl(null); const pollInterval = useRef(null);
const handleFileChange = (e) => { setFiles(Array.from(e.target.files)); };
const handleConvert = async () => { if (selectedTool.id === 'youtube' && !youtubeUrl) return; if (selectedTool.id !== 'youtube' && files.length === 0) return;
setStatus('processing'); setProgress(10); setLastLog('サーバーにリクエストを送信中...'); setError(null);
const formData = new FormData(); formData.append('tool_id', selectedTool.id); if (selectedTool.id === 'youtube') { formData.append('url', youtubeUrl); formData.append('output_format', youtubeFormat); // フォーマットを追加 formData.append('output_format', youtubeFormat); // フォーマット送信 } else { files.forEach(file => formData.append('files', file)); }
try { const res = await fetch('/api/convert', { method: 'POST', body: formData }); const { task_id } = await res.json(); const res = await fetch('/api/convert', { method: 'POST', body: formData, }); const data = await res.json(); setTaskId(data.task_id); startPolling(data.task_id); } catch (err) { setStatus('failed'); setError('サーバーとの通信に失敗しました。'); } };
const checkStatus = setInterval(async () => { const statusRes = await fetch(`/api/status/${task_id}`); const data = await statusRes.json(); const startPolling = (tid) => { if (pollInterval.current) clearInterval(pollInterval.current); pollInterval.current = setInterval(async () => { try { const res = await fetch(`/api/status/${tid}`); const data = await res.json(); setTaskState({ status: data.status, progress: data.progress || 0, last_log: data.last_log || data.error || "" }); setProgress(data.progress || 0); setLastLog(data.last_log || '');
if (data.status === 'completed') { clearInterval(checkStatus); setDownloadUrl(`/api/download/${task_id}`); clearInterval(pollInterval.current); setStatus('completed'); setDownloadUrl(`/api/download/${tid}`); } else if (data.status === 'failed') { clearInterval(checkStatus); clearInterval(pollInterval.current); setStatus('failed'); setError(data.error || '不明なエラーが発生しました。'); } }, 2000); } catch (e) { setTaskState({ status: 'failed', progress: 0, last_log: "通信エラーが発生しました" }); } } catch (err) { console.error('Polling error:', err); } }, 2000); };
const reset = () => { if (pollInterval.current) clearInterval(pollInterval.current); setSelectedTool(null); setFiles([]); setYoutubeUrl(''); setYoutubeFormat('mp3'); setTaskId(null); setStatus('idle'); setProgress(0); setLastLog(''); setDownloadUrl(null); setError(null); };
return ( <div class="flex flex-col min-h-screen"> <div className="max-w-4xl mx-auto px-4 py-12 flex-grow w-full"> {/* Header */} <div className="text-center mb-12"> <div className="inline-block p-3 bg-indigo-600 rounded-2xl mb-4 shadow-lg shadow-indigo-200"> <Icon name="zap" className="text-white w-8 h-8" /> </div> <h1 className="text-4xl font-black text-slate-900 mb-2 tracking-tight">MediaMaster Async</h1> <p className="text-slate-500 font-medium">高速・非同期メディア変換ツール</p> <div className="max-w-4xl mx-auto px-4 py-12"> {/* Header */} <header className="text-center mb-16"> <div className="inline-block p-3 bg-white shadow-xl rounded-3xl mb-6"> <Icon name="zap" className="w-10 h-10 text-emerald-500 fill-emerald-500" /> </div> <h1 className="text-5xl font-black text-slate-900 mb-4 tracking-tight">MediaMaster</h1> <p className="text-slate-500 font-medium text-lg">クラウドパワーでメディアを自由自在に変換</p> </header>
{/* Tool Selection or Action Area */} <div className="bg-white rounded-[2.5rem] shadow-2xl shadow-slate-200/60 p-8 border border-slate-100 mb-12"> {!selectedTool ? ( <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4"> {TOOLS.map(tool => ( <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6"> {TOOLS.map((tool) => ( <button key={tool.id} onClick={() => setSelectedTool(tool)} className="group bg-white p-6 rounded-3xl border border-slate-100 shadow-sm hover:shadow-xl hover:-translate-y-1 transition-all text-left relative overflow-hidden" className="group relative p-6 rounded-[2rem] border-2 border-slate-50 hover:border-emerald-500 transition-all duration-500 bg-slate-50/50 hover:bg-white hover:shadow-xl hover:shadow-emerald-500/10 text-left overflow-hidden" > <div className={`w-12 h-12 ${tool.color} rounded-2xl flex items-center justify-center mb-4 group-hover:scale-110 transition-transform`}> <Icon name={tool.icon} className="text-white w-6 h-6" /> <div className={`w-14 h-14 ${tool.color} rounded-2xl flex items-center justify-center mb-4 text-white shadow-lg group-hover:scale-110 transition-transform duration-500`}> <Icon name={tool.icon} className="w-7 h-7" /> </div> <h3 className="font-bold text-slate-900 mb-1">{tool.title}</h3> <p className="text-xs text-slate-500 leading-relaxed">{tool.description}</p> {tool.id === 'youtube' && ( <div className="absolute top-2 right-2 bg-amber-100 text-amber-700 text-[10px] px-2 py-0.5 rounded-full font-bold"> 要Colab設定 </div> )} <h3 className="text-xl font-bold text-slate-900 mb-2">{tool.title}</h3> <p className="text-slate-500 text-sm font-medium leading-relaxed">{tool.description}</p> </button> ))} </div> ) : ( <div className="bg-white rounded-[2.5rem] border border-slate-100 shadow-2xl overflow-hidden animate-in fade-in zoom-in duration-300"> <div className="p-8"> <button onClick={() => {setSelectedTool(null); setTaskState({status:'idle', progress:0}); setFiles([]); setYoutubeUrl("");}} className="flex items-center text-slate-400 hover:text-indigo-600 font-bold mb-8 transition-colors" > <Icon name="arrow-left" className="w-5 h-5 mr-2" /> ツール選択に戻る <div className="space-y-8 animate-in fade-in slide-in-from-bottom-4 duration-500"> <div className="flex items-center gap-4 border-b border-slate-100 pb-6"> <button onClick={reset} className="p-2 hover:bg-slate-100 rounded-xl transition-colors"> <Icon name="arrow-left" className="w-6 h-6 text-slate-400" /> </button>
<div className="flex items-center gap-4 mb-8"> <div className={`w-14 h-14 ${selectedTool.color} rounded-2xl flex items-center justify-center shadow-lg`}> <Icon name={selectedTool.icon} className="text-white w-7 h-7" /> </div> <div> <h2 className="text-2xl font-black text-slate-900">{selectedTool.title}</h2> <p className="text-slate-500 font-medium">{selectedTool.description}</p> </div> <div className={`w-12 h-12 ${selectedTool.color} rounded-xl flex items-center justify-center text-white shadow-md`}> <Icon name={selectedTool.icon} className="w-6 h-6" /> </div> <h2 className="text-2xl font-bold text-slate-900">{selectedTool.title}</h2> </div>
{status === 'idle' ? ( <div className="space-y-6"> {selectedTool.id === 'youtube' && ( {selectedTool.id === 'youtube' ? ( <div className="space-y-4"> <textarea className="w-full h-32 p-4 border border-slate-200 rounded-2xl focus:ring-4 focus:ring-emerald-500/10 focus:border-emerald-500 outline-none transition-all resize-none text-slate-600 font-medium"1 unmodified line value={youtubeUrl} onChange={(e) => setYoutubeUrl(e.target.value)} /> {/* 保存形式の選択を追加 */} {/* フォーマット選択肢の追加 */} <div className="flex gap-4 p-4 bg-slate-50 rounded-2xl border border-slate-100"> <span className="text-sm font-bold text-slate-500 flex items-center">保存形式:</span> <label className="flex items-center gap-2 cursor-pointer group"> <label className="flex items-center gap-2 cursor-pointer"> <input type="radio" name="format" 4 unmodified lines /> <span className={`text-sm font-bold ${youtubeFormat === 'mp3' ? 'text-emerald-600' : 'text-slate-400'}`}>MP3 (音声)</span> </label> <label className="flex items-center gap-2 cursor-pointer group"> <label className="flex items-center gap-2 cursor-pointer"> <input type="radio" name="format" 6 unmodified lines </label> </div> </div> )} {taskState.status !== 'idle' && ( <div className="bg-slate-50 rounded-2xl p-6 border border-slate-100"> <div className="flex justify-between text-sm font-bold mb-3"> <span className="text-indigo-600">{taskState.last_log}</span> <span className="text-slate-400">{taskState.progress}%</span> </div> <div className="w-full bg-slate-200 rounded-full h-3 overflow-hidden"> <div className="h-full bg-indigo-600 transition-all duration-500 shadow-[0_0_10px_rgba(79,70,229,0.5)]" style={{ width: `${taskState.progress}%` }} ></div> ) : ( <div className="relative group"> <input type="file" multiple onChange={handleFileChange} className="absolute inset-0 w-full h-full opacity-0 cursor-pointer z-10" /> <div className="border-4 border-dashed border-slate-100 rounded-[2rem] p-12 text-center group-hover:border-emerald-500/30 group-hover:bg-emerald-50/30 transition-all duration-500"> <div className="w-20 h-20 bg-emerald-100 rounded-3xl flex items-center justify-center mx-auto mb-6 text-emerald-500 group-hover:scale-110 transition-transform duration-500 shadow-inner"> <Icon name="upload-cloud" className="w-10 h-10" /> </div> <p className="text-xl font-bold text-slate-900 mb-2"> {files.length > 0 ? `${files.length}個のファイルを選択中` : 'ファイルをドラッグ&ドロップ'} </p> <p className="text-slate-400 font-medium">または、クリックしてファイルを選択</p> </div> </div> )}
<div className="flex gap-4"> <button onClick={startProcessing} disabled={taskState.status === 'processing' || taskState.status === 'uploading' || (selectedTool.id === 'youtube' && !youtubeUrl)} className="flex-1 bg-slate-900 text-white py-4 rounded-2xl font-bold hover:bg-indigo-600 disabled:bg-slate-200 transition-all shadow-lg" > 処理開始 </button> {downloadUrl && ( <a href={downloadUrl} className="flex-1 bg-emerald-500 text-white py-4 rounded-2xl font-bold text-center hover:bg-emerald-600 animate-pulse shadow-lg" > ダウンロード </a> <button onClick={handleConvert} disabled={(selectedTool.id === 'youtube' ? !youtubeUrl : files.length === 0)} className="w-full bg-slate-900 text-white py-5 rounded-2xl font-bold text-xl hover:bg-emerald-500 hover:shadow-2xl hover:shadow-emerald-500/20 disabled:opacity-30 disabled:hover:bg-slate-900 transition-all duration-500 active:scale-95" > 処理を開始する </button> </div> ) : ( <div className="py-12 px-4"> <div className="max-w-md mx-auto space-y-8"> {/* Status Header */} <div className="text-center"> <div className={`inline-flex items-center justify-center w-20 h-20 rounded-3xl mb-6 shadow-xl ${ status === 'completed' ? 'bg-emerald-500' : status === 'failed' ? 'bg-rose-500' : 'bg-emerald-100' }`}> {status === 'completed' ? <Icon name="check" className="w-10 h-10 text-white" /> : status === 'failed' ? <Icon name="x" className="w-10 h-10 text-white" /> : <Icon name="loader-2" className="w-10 h-10 text-emerald-500 animate-spin" />} </div> <h3 className="text-2xl font-black text-slate-900 mb-2"> {status === 'completed' ? '処理完了!' : status === 'failed' ? 'エラーが発生しました' : '処理を進行中'} </h3> <p className="text-slate-500 font-bold">{lastLog}</p> </div>
{/* Progress Bar */} {status !== 'failed' && ( <div className="space-y-3"> <div className="flex justify-between text-sm font-black text-slate-900 px-1"> <span>進行状況</span> <span>{progress}%</span> </div> <div className="h-4 bg-slate-100 rounded-full overflow-hidden shadow-inner"> <div className="h-full bg-emerald-500 transition-all duration-500 rounded-full shadow-[0_0_20px_rgba(16,185,129,0.5)]" style={{ width: `${progress}%` }} /> </div> </div> )}
{/* Error Message */} {error && ( <div className="p-4 bg-rose-50 border border-rose-100 rounded-2xl text-rose-600 font-bold text-center"> {error} </div> )}
{/* Buttons */} <div className="flex gap-4"> <button onClick={reset} className="flex-1 px-6 py-4 rounded-2xl font-bold text-slate-600 border-2 border-slate-100 hover:bg-slate-50 transition-all" > 戻る </button> {downloadUrl && ( <a href={downloadUrl} className="flex-1 bg-emerald-500 text-white py-4 rounded-2xl font-bold text-center hover:bg-emerald-600 animate-pulse shadow-lg" > ダウンロード </a> )} </div> </div> </div> </div> )} </div> )} </div>index.html+186-108