@@ -858,9 +858,9 @@ def get_subtitle_languages_and_duration(self, media_filepath):
858
858
media_filepath
859
859
]
860
860
861
+ Config .enableRedirection ()
861
862
FFprobe .execute (command )
862
863
output = Config .getLastCommandOutput ()
863
-
864
864
metadata = json .loads (output )
865
865
streams = metadata ['streams' ]
866
866
duration = int (float (metadata ['format' ]['duration' ])* 1000 )
@@ -1396,9 +1396,9 @@ def get_subtitle_languages_and_duration(self, media_filepath):
1396
1396
media_filepath
1397
1397
]
1398
1398
1399
+ Config .enableRedirection ()
1399
1400
FFprobe .execute (command )
1400
1401
output = Config .getLastCommandOutput ()
1401
-
1402
1402
metadata = json .loads (output )
1403
1403
streams = metadata ['streams' ]
1404
1404
duration = int (float (metadata ['format' ]['duration' ])* 1000 )
@@ -1476,9 +1476,9 @@ def get_subtitle_languages_and_duration(self, media_filepath):
1476
1476
media_filepath
1477
1477
]
1478
1478
1479
+ Config .enableRedirection ()
1479
1480
FFprobe .execute (command )
1480
1481
output = Config .getLastCommandOutput ()
1481
-
1482
1482
metadata = json .loads (output )
1483
1483
streams = metadata ['streams' ]
1484
1484
duration = int (float (metadata ['format' ]['duration' ])* 1000 )
@@ -1569,9 +1569,9 @@ def get_subtitle_languages_and_duration(self, media_filepath):
1569
1569
media_filepath
1570
1570
]
1571
1571
1572
+ Config .enableRedirection ()
1572
1573
FFprobe .execute (command )
1573
1574
output = Config .getLastCommandOutput ()
1574
-
1575
1575
metadata = json .loads (output )
1576
1576
streams = metadata ['streams' ]
1577
1577
duration = int (float (metadata ['format' ]['duration' ])* 1000 )
@@ -1636,6 +1636,89 @@ def __call__(self, media_filepath):
1636
1636
return
1637
1637
1638
1638
1639
+ def check_file_type (media_filepath , error_messages_callback = None ):
1640
+ if not os .path .isfile (media_filepath ):
1641
+ if error_messages_callback :
1642
+ error_messages_callback (f"The given file does not exist: '{ media_filepath } '" )
1643
+ else :
1644
+ print (f"The given file does not exist: '{ media_filepath } '" )
1645
+ raise Exception (f"Invalid file: '{ media_filepath } '" )
1646
+
1647
+ try :
1648
+ command = [
1649
+ '-hide_banner' ,
1650
+ '-v' , 'error' ,
1651
+ '-loglevel' , 'error' ,
1652
+ '-of' , 'json' ,
1653
+ '-show_entries' ,
1654
+ 'format:stream' ,
1655
+ media_filepath
1656
+ ]
1657
+
1658
+ Config .enableRedirection ()
1659
+ FFprobe .execute (command )
1660
+ output = Config .getLastCommandOutput ()
1661
+ metadata = json .loads (output )
1662
+ streams = metadata ['streams' ]
1663
+
1664
+ for stream in streams :
1665
+ if 'codec_type' in stream and stream ['codec_type' ] == 'audio' :
1666
+ return 'audio'
1667
+ elif 'codec_type' in stream and stream ['codec_type' ] == 'video' :
1668
+ return 'video'
1669
+
1670
+ except Exception as e :
1671
+ if error_messages_callback :
1672
+ error_messages_callback (e )
1673
+ else :
1674
+ print (e )
1675
+
1676
+ return None
1677
+
1678
+
1679
+ def has_subtitles (media_filepath , error_messages_callback = None ):
1680
+ if not os .path .isfile (media_filepath ):
1681
+ if error_messages_callback :
1682
+ error_messages_callback (f"The given file does not exist: '{ media_filepath } '" )
1683
+ else :
1684
+ print (f"The given file does not exist: '{ media_filepath } '" )
1685
+ raise Exception (f"Invalid file: '{ media_filepath } '" )
1686
+
1687
+ try :
1688
+ ffmpeg_cmd = [
1689
+ 'ffmpeg' ,
1690
+ '-hide_banner' ,
1691
+ '-v' , 'error' ,
1692
+ '-loglevel' , 'error' ,
1693
+ '-y' ,
1694
+ '-i' , media_filepath ,
1695
+ '-map' , '0:s:0' ,
1696
+ '-c:s' , 'text' ,
1697
+ subtitle_filepath
1698
+ ]
1699
+
1700
+ Config .enableRedirection ()
1701
+ FFmpeg .execute (ffmpeg_command )
1702
+ output = Config .getLastCommandOutput ()
1703
+
1704
+ if os .path .isfile (subtitle_filepath ):
1705
+ srt_file_reader = SRTFileReader ()
1706
+ timed_subtitles = srt_file_reader (subtitle_filepath )
1707
+ else :
1708
+ timed_subtitles = None
1709
+
1710
+ if timed_subtitles :
1711
+ return True # Subtitles detected
1712
+ else :
1713
+ return False # No subtitles detected
1714
+
1715
+ except Exception as e :
1716
+ if error_messages_callback :
1717
+ error_messages_callback (e )
1718
+ else :
1719
+ print (e )
1720
+ return False
1721
+
1639
1722
1640
1723
def is_same_language (lang1 , lang2 ):
1641
1724
return lang1 .split ("-" )[0 ] == lang2 .split ("-" )[0 ]
@@ -1740,6 +1823,8 @@ def transcribe(src, dst, media_filepath, media_file_display_name, subtitle_forma
1740
1823
base , ext = os .path .splitext (media_filepath )
1741
1824
media_file_display_name = os .path .basename (media_filepath ).split ('/' )[- 1 ]
1742
1825
print (f"media_file_display_name = '{ media_file_display_name } '" )
1826
+ media_type = check_file_type (media_filepath )
1827
+ print (f"media_type = '{ media_type } '" )
1743
1828
media_file_format = ext [1 :]
1744
1829
print (f"media_file_format = '{ media_file_format } '" )
1745
1830
@@ -1769,7 +1854,7 @@ def transcribe(src, dst, media_filepath, media_file_display_name, subtitle_forma
1769
1854
activity .runOnUiThread (appendText (textview_output_messages , "Running python script...\n " ))
1770
1855
1771
1856
# CHECKING SUBTITLE STREAMS
1772
- if force_recognize == False :
1857
+ if media_type == "video" and force_recognize == False :
1773
1858
1774
1859
src_subtitle_filepath = None
1775
1860
dst_subtitle_filepath = None
@@ -2404,35 +2489,35 @@ def transcribe(src, dst, media_filepath, media_file_display_name, subtitle_forma
2404
2489
os .mkdir (subtitle_folder_name )
2405
2490
2406
2491
base , ext = os .path .splitext (media_filepath )
2407
-
2408
2492
if ext [1 :] == "ts" :
2409
2493
media_file_format = "mp4"
2410
2494
else :
2411
2495
media_file_format = ext [1 :]
2412
2496
2413
- tmp_force_recognize_media_filepath = f"{ subtitle_folder_name + os .sep + media_file_display_name [:- len (media_file_format )- 1 ]} .tmp.force.recognize.{ media_file_format } "
2497
+ if has_subtitles (media_filepath ):
2498
+ tmp_force_recognize_media_filepath = f"{ subtitle_folder_name + os .sep + media_file_display_name [:- len (media_file_format )- 1 ]} .tmp.force.recognize.{ media_file_format } "
2414
2499
2415
- activity .runOnUiThread (setVisibility (textview_progress , progress_bar , textview_percentage , textview_time , View .VISIBLE ))
2416
- print ("Removing subtitle streams..." )
2417
- activity .runOnUiThread (appendText (textview_output_messages , "Removing subtitle streams...\n " ))
2418
- remove_subtitle_streams_start_time = time .time ()
2419
- pbar (0 , remove_subtitle_streams_start_time , 100 , "Removing subtitle streams" , activity , textview_progress , progress_bar , textview_percentage , textview_time )
2420
- Config .enableRedirection ()
2500
+ activity .runOnUiThread (setVisibility (textview_progress , progress_bar , textview_percentage , textview_time , View .VISIBLE ))
2501
+ print ("Removing subtitle streams..." )
2502
+ activity .runOnUiThread (appendText (textview_output_messages , "Removing subtitle streams...\n " ))
2503
+ remove_subtitle_streams_start_time = time .time ()
2504
+ pbar (0 , remove_subtitle_streams_start_time , 100 , "Removing subtitle streams" , activity , textview_progress , progress_bar , textview_percentage , textview_time )
2505
+ Config .enableRedirection ()
2421
2506
2422
- subtitle_remover = MediaSubtitleRemover (output_path = tmp_force_recognize_media_filepath , start_time = remove_subtitle_streams_start_time , activity = activity , textview_progress = textview_progress , progress_bar = progress_bar , textview_percentage = textview_percentage , textview_time = textview_time )
2423
- tmp_output = subtitle_remover (media_filepath )
2507
+ subtitle_remover = MediaSubtitleRemover (output_path = tmp_force_recognize_media_filepath , start_time = remove_subtitle_streams_start_time , activity = activity , textview_progress = textview_progress , progress_bar = progress_bar , textview_percentage = textview_percentage , textview_time = textview_time )
2508
+ tmp_output = subtitle_remover (media_filepath )
2424
2509
2425
- Config .disableRedirection ()
2426
- pbar (100 , remove_subtitle_streams_start_time , 100 , "Removing subtitle streams" , activity , textview_progress , progress_bar , textview_percentage , textview_time )
2427
- time .sleep (1 )
2510
+ Config .disableRedirection ()
2511
+ pbar (100 , remove_subtitle_streams_start_time , 100 , "Removing subtitle streams" , activity , textview_progress , progress_bar , textview_percentage , textview_time )
2512
+ time .sleep (1 )
2428
2513
2429
- print (f"Subtitle streams removed" )
2430
- activity .runOnUiThread (appendText (textview_output_messages , "Subtitle streams removed\n " ))
2431
- activity .runOnUiThread (setVisibility (textview_progress , progress_bar , textview_percentage , textview_time , View .INVISIBLE ))
2514
+ print (f"Subtitle streams removed" )
2515
+ activity .runOnUiThread (appendText (textview_output_messages , "Subtitle streams removed\n " ))
2516
+ activity .runOnUiThread (setVisibility (textview_progress , progress_bar , textview_percentage , textview_time , View .INVISIBLE ))
2432
2517
2433
- if os .path .isfile (tmp_output ):
2434
- shutil .copy (tmp_output , media_filepath )
2435
- os .remove (tmp_output )
2518
+ if os .path .isfile (tmp_output ):
2519
+ shutil .copy (tmp_output , media_filepath )
2520
+ os .remove (tmp_output )
2436
2521
2437
2522
2438
2523
# TRANSCRIBE PART
@@ -2713,9 +2798,17 @@ def transcribe(src, dst, media_filepath, media_file_display_name, subtitle_forma
2713
2798
2714
2799
#activity.runOnUiThread(setVisibility(textview_progress, progress_bar, textview_percentage, textview_time, View.VISIBLE))
2715
2800
2801
+ print (f"media_type = { media_type } " )
2802
+ print (f"embed_src = { embed_src } " )
2803
+ print (f"embed_dst = { embed_dst } " )
2804
+
2716
2805
if is_same_language (src , dst ):
2717
2806
2718
- if embed_src == True :
2807
+ if media_type == "audio" :
2808
+ print ("Subtitles can only be embedded into video file, not audio file" )
2809
+ activity .runOnUiThread (appendText (textview_output_messages , "Subtitles can only be embedded into video file, not audio file\n " ))
2810
+
2811
+ if media_type == "video" and embed_src == True :
2719
2812
try :
2720
2813
print (f"Embedding '{ ffmpeg_src_language_code } ' subtitles..." )
2721
2814
activity .runOnUiThread (appendText (textview_output_messages , f"Embedding '{ ffmpeg_src_language_code } ' subtitles...\n " ))
@@ -2759,7 +2852,11 @@ def transcribe(src, dst, media_filepath, media_file_display_name, subtitle_forma
2759
2852
2760
2853
elif not is_same_language (src , dst ):
2761
2854
2762
- if embed_src == True and embed_dst == True :
2855
+ if media_type == "audio" :
2856
+ print ("Subtitles can only be embedded into video file, not audio file" )
2857
+ activity .runOnUiThread (appendText (textview_output_messages , "Subtitles can only be embedded into video file, not audio file\n " ))
2858
+
2859
+ if media_type == "video" and embed_src == True and embed_dst == True :
2763
2860
try :
2764
2861
print (f"Embedding '{ ffmpeg_src_language_code } ' subtitles..." )
2765
2862
activity .runOnUiThread (appendText (textview_output_messages , f"Embedding '{ ffmpeg_src_language_code } ' subtitles...\n " ))
@@ -2828,7 +2925,7 @@ def transcribe(src, dst, media_filepath, media_file_display_name, subtitle_forma
2828
2925
pool .join ()
2829
2926
return
2830
2927
2831
- elif embed_src == True and embed_dst == False :
2928
+ elif media_type == "video" and embed_src == True and embed_dst == False :
2832
2929
try :
2833
2930
2834
2931
print (f"Embedding '{ ffmpeg_src_language_code } ' subtitles..." )
@@ -2879,7 +2976,7 @@ def transcribe(src, dst, media_filepath, media_file_display_name, subtitle_forma
2879
2976
pool .join ()
2880
2977
return
2881
2978
2882
- elif embed_src == False and embed_dst == True :
2979
+ elif media_type == "video" and embed_src == False and embed_dst == True :
2883
2980
try :
2884
2981
2885
2982
print (f"Embedding '{ ffmpeg_dst_language_code } ' subtitles..." )
0 commit comments