Home

Add

Edit

Without Linenumbers

Code in Textfield

Download

  1. '##############################################################################################################################################################
    
  2. '##############################################################################################################################################################
    
  3. '### ViCut - V1.00.0 R0 - Beta
    
  4. '##############################################################################################################################################################
    
  5. '##############################################################################################################################################################
    
  6. '### (c) Copyright 2011 By.: Martin Wiemann
    
  7. '##############################################################################################################################################################
    
  8. '### Date of Idea:  2011.03.25 - 20:53:49
    
  9. '### Autor:         DeltaLab's Germany [Experimental Computing]
    
  10. '###                Martin Wiemann
    
  11. '### Contact:       Admin@MLN.ath.cx   /   IRC://MLN.ath.cx/#mln
    
  12. '##############################################################################################################################################################
    
  13. '##############################################################################################################################################################
    
  14. 
    
  15. 
    
  16. 
    
  17. '##############################################################################################################################################################
    
  18. Dim Shared G_TranscodeMovie_Progress As EWH_Control_Type Ptr
    
  19. 
    
  20. 
    
  21. 
    
  22. '##############################################################################################################################################################
    
  23. Dim Shared ViCut_FFConv_ThreadMutex As Any Ptr
    
  24. Dim Shared ViCut_FFConv_ThreadFFDone As Ubyte
    
  25. Dim Shared ViCut_FFConv_Count As Uinteger
    
  26. Dim Shared ViCut_FFConv_DatSize As Uinteger
    
  27. Dim Shared ViCut_FFConv_Source As String
    
  28. Dim Shared ViCut_FFConv_TargetName As String
    
  29. Dim Shared ViCut_FFConv_TargetPath As String
    
  30. Dim Shared ViCut_FFConv_ResizeW As Uinteger
    
  31. Dim Shared ViCut_FFConv_ResizeH As Uinteger
    
  32. 
    
  33. 
    
  34. 
    
  35. '###############################################################################################################################################
    
  36. Sub ViCut_FFConv_Thread_Decode_FF()
    
  37. If (ViCut_FFConv_ResizeW <> 0) And (ViCut_FFConv_ResizeH <> 0) Then
    
  38.     Shell "ffmpeg -i " & ViCut_FFConv_Source & " -an -vcodec mjpeg -qmin 1 -qscale 1 -s " & Str(ViCut_FFConv_ResizeW) & "x" & Str(ViCut_FFConv_ResizeH) & " temp/img/img_%10d.jpg &> /dev/null"
    
  39. Else
    
  40.     Shell "ffmpeg -i " & ViCut_FFConv_Source & " -an -vcodec mjpeg -qmin 1 -qscale 1 temp/img/img_%10d.jpg &> /dev/null"
    
  41. End If
    
  42. Mutexlock(ViCut_FFConv_ThreadMutex)
    
  43. ViCut_FFConv_ThreadFFDone = 1
    
  44. Mutexunlock(ViCut_FFConv_ThreadMutex)
    
  45. End Sub
    
  46. 
    
  47. 
    
  48. 
    
  49. '###############################################################################################################################################
    
  50. Sub ViCut_FFConv_Thread_Pack()
    
  51. Mutexlock(ViCut_FFConv_ThreadMutex)
    
  52. Mutexlock(G_FileIOMutex)
    
  53. Kill ViCut_FFConv_TargetPath & ViCut_FFConv_TargetName & ".vcl"
    
  54. Kill ViCut_FFConv_TargetPath & ViCut_FFConv_TargetName & ".vcv"
    
  55. Kill ViCut_FFConv_TargetPath & ViCut_FFConv_TargetName & ".vca"
    
  56. Kill ViCut_FFConv_TargetPath & ViCut_FFConv_TargetName & ".vci"
    
  57. Dim XFN As Integer = Freefile
    
  58. Open ViCut_FFConv_TargetPath & ViCut_FFConv_TargetName & ".vcl" For Binary As #XFN
    
  59. Dim XIXFN As Integer = Freefile
    
  60. Open ViCut_FFConv_TargetPath & ViCut_FFConv_TargetName & ".vcv" For Binary As #XIXFN
    
  61. Mutexunlock(G_FileIOMutex)
    
  62. Mutexunlock(ViCut_FFConv_ThreadMutex)
    
  63. Dim N As String
    
  64. Dim C As Uinteger
    
  65. Dim Z As Uinteger = 1
    
  66. Dim T As String
    
  67. Dim TFN As Integer
    
  68. Dim TFN1 As Integer
    
  69. Dim XMax As Uinteger
    
  70. Dim D As String
    
  71. Dim T1 As String
    
  72. Dim TSize As Uinteger
    
  73. Do
    
  74.     C = 0
    
  75.     Mutexlock(G_FileIOMutex)
    
  76.     N = Dir("temp/img/*", -1)
    
  77.     Do Until N = ""
    
  78.         If (N <> ".") And (N <> "..") Then C += 1
    
  79.         N = Dir("", -1)
    
  80.     Loop
    
  81.     Mutexunlock(G_FileIOMutex)
    
  82.     Do
    
  83. '       If C <= 1 Then
    
  84.         If Dir("temp/img/img_" & String(10 - Len(Str(Z + 1)), 48) & Str(Z + 1) & ".jpg") = "" Then
    
  85.             Mutexlock(ViCut_FFConv_ThreadMutex)
    
  86.             If ViCut_FFConv_ThreadFFDone = 0 Then Mutexunlock(ViCut_FFConv_ThreadMutex): Exit Do
    
  87.             Mutexunlock(ViCut_FFConv_ThreadMutex)
    
  88.         End If
    
  89.         T = "img_" & String(10 - Len(Str(Z)), 48) & Str(Z) & ".jpg"
    
  90.         If Dir("temp/img/" & T) = "" Then Exit Do
    
  91.         Mutexlock(G_FileIOMutex)
    
  92.         TFN = Freefile
    
  93.         Open "temp/img/" & T For Binary As #TFN
    
  94.         Mutexunlock(G_FileIOMutex)
    
  95.         XMax = Lof(TFN)
    
  96.         D = Space(8092)
    
  97.         For X As Uinteger = 1 To XMax Step 8092
    
  98.             If (XMax - X) < 8092 Then D = Space(XMax - X + 1)
    
  99.             Get #TFN, X, D
    
  100.             Print #XIXFN, D;
    
  101.         Next
    
  102.         If Z = 1 Then
    
  103.             Mutexlock(G_FileIOMutex)
    
  104.             TFN1 = Freefile
    
  105.             Open ViCut_FFConv_TargetPath & ViCut_FFConv_TargetName & ".vci" For Binary As #TFN1
    
  106.             Mutexunlock(G_FileIOMutex)
    
  107.             D = Space(8092)
    
  108.             For X As Uinteger = 1 To XMax Step 8092
    
  109.                 If (XMax - X) < XMax Then D = Space(XMax - X + 1)
    
  110.                 Get #TFN, X, D
    
  111.                 Print #TFN1, D;
    
  112.             Next
    
  113.             Close #TFN1
    
  114.         End If
    
  115.         Close #TFN
    
  116.         T1 = Space(4)
    
  117.         *Cast(Uinteger Ptr, Strptr(T1)) = TSize
    
  118.         Print #XFN, T1;
    
  119.         *Cast(Uinteger Ptr, Strptr(T1)) = XMax
    
  120.         Print #XFN, T1;
    
  121. '       Print Str(Z) & " " & Str(TSize) & " " & Str(XMax)
    
  122.         TSize += XMax
    
  123.         Mutexlock(ViCut_FFConv_ThreadMutex)
    
  124.         ViCut_FFConv_DatSize = TSize
    
  125.         Mutexunlock(ViCut_FFConv_ThreadMutex)
    
  126.         Mutexlock(G_FileIOMutex)
    
  127.         Kill "temp/img/" & T
    
  128.         Mutexunlock(G_FileIOMutex)
    
  129.         Z += 1
    
  130.         Sleep 1, 1
    
  131.     Loop
    
  132.     Mutexlock(ViCut_FFConv_ThreadMutex)
    
  133.     ViCut_FFConv_Count = Z
    
  134.     Mutexunlock(ViCut_FFConv_ThreadMutex)
    
  135.     If C = 0 Then
    
  136.         Mutexlock(ViCut_FFConv_ThreadMutex)
    
  137.         If ViCut_FFConv_ThreadFFDone = 1 Then Exit Do
    
  138.         Mutexunlock(ViCut_FFConv_ThreadMutex)
    
  139.     End If
    
  140.     Sleep 1, 1
    
  141. Loop
    
  142. If ViCut_FFConv_Count > 0 Then ViCut_FFConv_Count -= 1
    
  143. Mutexunlock(ViCut_FFConv_ThreadMutex)
    
  144. Close #XIXFN
    
  145. Close #XFN
    
  146. Mutexlock(G_FileIOMutex)
    
  147. XFN = Freefile
    
  148. Open ViCut_FFConv_TargetPath & ViCut_FFConv_TargetName & ".vch" For Append As #XFN
    
  149. Mutexunlock(G_FileIOMutex)
    
  150. Mutexlock(ViCut_FFConv_ThreadMutex)
    
  151. Print #XFN, "FrameCount=" & ViCut_FFConv_Count
    
  152. Print #XFN, "DatSize=" & ViCut_FFConv_DatSize
    
  153. ViCut_FFConv_ThreadFFDone = 2
    
  154. Mutexunlock(ViCut_FFConv_ThreadMutex)
    
  155. Close #XFN
    
  156. End Sub
    
  157. 
    
  158. 
    
  159. 
    
  160. '##############################################################################################################################################################
    
  161. Sub ViCut_TranscodeMovie_DoTranscode(V_SourcePathFileName As String, V_ResizeW As Uinteger, V_ResizeH As Uinteger, V_TargetPath As String, Byref R_IndexName As String, Byref R_FrameCount As Uinteger, Byref R_FrameRate As Uinteger)
    
  162. Mutexlock(G_FileIOMutex)
    
  163. Mkdir "temp"
    
  164. Mkdir "temp/img"
    
  165. Mutexunlock(G_FileIOMutex)
    
  166. ViCut_FFConv_Source = V_SourcePathFileName
    
  167. ViCut_FFConv_TargetPath = V_TargetPath
    
  168. ViCut_FFConv_ResizeW = V_ResizeW
    
  169. ViCut_FFConv_ResizeH = V_ResizeH
    
  170. Dim DD() As String
    
  171. Dim DC As Uinteger
    
  172. Dim DX As Uinteger
    
  173. Mutexlock(G_FileIOMutex)
    
  174. Dim N As String = Dir("temp/img/*", -1)
    
  175. Do Until N = ""
    
  176.     If (N <> ".") And (N <> "..") Then
    
  177.         DC += 1
    
  178.         If DC > DX Then DX += 10: Redim Preserve DD(DX) As String
    
  179.         DD(DC) = N
    
  180.     End If
    
  181.     N = Dir("", -1)
    
  182. Loop
    
  183. For X As Uinteger = 1 To DC
    
  184.     Kill "temp/img/" & DD(X)
    
  185. Next
    
  186. Mutexunlock(G_FileIOMutex)
    
  187. Dim XPos As Uinteger
    
  188. Dim T As String
    
  189. Dim T1 As String
    
  190. Dim T2 As String
    
  191. Dim TFrames As Uinteger
    
  192. Dim TFrameRate As Uinteger = 0
    
  193. Mutexlock(G_FileIOMutex)
    
  194. Kill "temp/ffconvout.tmp"
    
  195. Mutexunlock(G_FileIOMutex)
    
  196. Shell("ffmpeg -i " & ViCut_FFConv_Source & " &> temp/ffconvout.tmp")
    
  197. Mutexlock(G_FileIOMutex)
    
  198. Dim XFN As Integer = Freefile
    
  199. Open "temp/ffconvout.tmp" For Binary As #XFN
    
  200. Mutexunlock(G_FileIOMutex)
    
  201. Do Until Eof(XFN)
    
  202.     Line Input #XFN, T
    
  203.     If Left(T, 11) = "  Duration:" Then
    
  204.         T = Mid(T, 12)
    
  205.         XPos = Instr(1, T, ",")
    
  206.         If XPos > 0 Then T2 = Trim(Left(T, XPos - 1))
    
  207.     Else
    
  208.         If Left(T, 11) = "    Stream " Then
    
  209.             T = Mid(T, 12)
    
  210.             XPos = Instr(1, T, ":")
    
  211.             If XPos > 0 Then
    
  212.                 T = Trim(Mid(T, XPos + 1))
    
  213.                 XPos = Instr(1, T, ":")
    
  214.                 If XPos > 0 Then
    
  215.                     If Trim(Left(T, XPos - 1)) = "Video" Then
    
  216.                         T1 = Trim(Mid(T, XPos + 1))
    
  217.                     End If
    
  218.                 End If
    
  219.             End If
    
  220.         End If
    
  221.     End If
    
  222. Loop
    
  223. Close #XFN
    
  224. Mutexlock(G_FileIOMutex)
    
  225. Kill "temp/ffconvout.tmp"
    
  226. Mutexunlock(G_FileIOMutex)
    
  227. T = T1
    
  228. XPos = Instrrev(T, "fps")
    
  229. If XPos > 0 Then
    
  230.     T = Trim(Left(T, XPos - 1))
    
  231.     XPos = Instrrev(T, ",")
    
  232.     If XPos > 0 Then
    
  233.         T = Trim(Mid(T, XPos + 1))
    
  234.         XPos = Instr(1, T, " ")
    
  235.         If XPos > 0 Then T = Trim(Left(T, XPos - 1))
    
  236.         TFrameRate = Cuint(Cdbl(T))
    
  237.     End If
    
  238. End If
    
  239. If TFrameRate > 0 Then
    
  240.     T = T2
    
  241.     XPos = Instrrev(T, ".")
    
  242.     If XPos > 0 Then
    
  243.         TFrames = Cuint(Mid(T, XPos + 1))
    
  244.         T = Left(T, XPos - 1)
    
  245.         XPos = Instrrev(T, ":")
    
  246.         If XPos > 0 Then
    
  247.             TFrames += Cuint(Mid(T, XPos + 1)) * TFrameRate
    
  248.             T = Left(T, XPos - 1)
    
  249.             XPos = Instrrev(T, ":")
    
  250.             If XPos > 0 Then TFrames += Cuint(Mid(T, XPos + 1)) * 60 * TFrameRate
    
  251.         End If
    
  252.     End If
    
  253. End If
    
  254. T = V_SourcePathFileName
    
  255. XPos = Instrrev(T, "/")
    
  256. If XPos > 0 Then T = Mid(T, XPos + 1)
    
  257. If TFrameRate = 0 Then TFrameRate = 25
    
  258. ViCut_FFConv_TargetName = T
    
  259. ViCut_FFConv_TargetName = T
    
  260. R_FrameCount = TFrames
    
  261. R_FrameRate = TFrameRate
    
  262. R_IndexName = ViCut_FFConv_TargetName
    
  263. Mutexlock(G_FileIOMutex)
    
  264. Kill ViCut_FFConv_TargetPath & ViCut_FFConv_TargetName & ".vcl"
    
  265. Kill ViCut_FFConv_TargetPath & ViCut_FFConv_TargetName & ".vch"
    
  266. XFN = Freefile
    
  267. Open ViCut_FFConv_TargetPath & ViCut_FFConv_TargetName & ".vch" For Append As #XFN
    
  268. Mutexunlock(G_FileIOMutex)
    
  269. Print #XFN, "SourceFile=" & V_SourcePathFileName
    
  270. Print #XFN, "IndexName=" & ViCut_FFConv_TargetName
    
  271. Print #XFN, "FrameRate=" & TFrameRate
    
  272. Close #XFN
    
  273. ViCut_FFConv_ThreadFFDone = 0
    
  274. ViCut_FFConv_Count = 0
    
  275. ViCut_FFConv_DatSize = 0
    
  276. Mutexlock(ViCut_FFConv_ThreadMutex)
    
  277. Dim TThreadIX As Any Ptr = Threadcreate(Cast(Any Ptr, @ViCut_FFConv_Thread_Pack))
    
  278. Dim TThreadFF As Any Ptr = Threadcreate(Cast(Any Ptr, @ViCut_FFConv_Thread_Decode_FF))
    
  279. Mutexunlock(ViCut_FFConv_ThreadMutex)
    
  280. End Sub
    
  281. 
    
  282. 
    
  283. 
    
  284. '##############################################################################################################################################################
    
  285. Sub ViCut_TranscodeMovie_Run(V_SourcePathFileName As String, V_ResizeW As Uinteger, V_ResizeH As Uinteger, Byref R_IndexName As String, Byref R_FrameCount As Uinteger, Byref R_FrameRate As Uinteger, Byref R_DatFilesize As Uinteger)
    
  286. With *G_TranscodeMovie_Progress
    
  287.     .Text = "Preparing transcode..."
    
  288.     .Max = 0
    
  289.     .Value = 0
    
  290. End With
    
  291. G_TranscodeMovie_Window->Show(EWH_YN_Yes)
    
  292. EWH_Window_ThreadSleep(250)
    
  293. 
    
  294. Mkdir "transcode/"
    
  295. 
    
  296. G_TranscodeMovie_Progress->Text = "Begin transcode..."
    
  297. ViCut_TranscodeMovie_DoTranscode(V_SourcePathFileName, V_ResizeW, V_ResizeH, "transcode/", R_IndexName, R_FrameCount, R_FrameRate)
    
  298. 
    
  299. Dim TPrep As Ubyte
    
  300. Dim TLFrame As Uinteger
    
  301. Dim TTimeS As Double = Timer()
    
  302. Dim TTimeR As Uinteger
    
  303. Dim TTot As Double
    
  304. Dim TT As String
    
  305. Do Until EWH_Window_CheckEvents(@EWH_Term) = 0
    
  306.     Mutexlock(ViCut_FFConv_ThreadMutex)
    
  307.     If ViCut_FFConv_ThreadFFDone = 2 Then Mutexunlock(ViCut_FFConv_ThreadMutex): Exit Do
    
  308.     If TPrep = 0 Then
    
  309.         If R_FrameCount > 0 Then
    
  310.             G_TranscodeMovie_Progress->Max = R_FrameCount
    
  311.             TPrep = 1
    
  312.         Elseif ViCut_FFConv_Count > 0 Then
    
  313.             G_TranscodeMovie_Progress->Max = R_FrameCount
    
  314.             TPrep = 2
    
  315.         End If
    
  316.     Elseif TPrep = 1 Then
    
  317.         If TLFrame <> ViCut_FFConv_Count Then
    
  318.             If TTot < Timer() Then
    
  319.                 TTimeR = Fix(R_FrameCount / (ViCut_FFConv_Count / (Timer() - TTimeS)) - (Timer() - TTimeS))
    
  320.                 TT = Format(Int(TTimeR / 3600), "00")
    
  321.                 TTimeR Mod= 3600
    
  322.                 TT += ":" & Format(Int(TTimeR / 60), "00")
    
  323.                 TTimeR Mod= 60
    
  324.                 TT += ":" & Format(TTimeR, "00")
    
  325.                 TTot = Timer() + 1
    
  326.             End If
    
  327.             G_TranscodeMovie_Progress->Value = ViCut_FFConv_Count
    
  328.             G_TranscodeMovie_Progress->Text = Str(R_FrameCount) & " / " & Space(Len(Str(R_FrameCount)) - Len(Str(ViCut_FFConv_Count))) & Str(ViCut_FFConv_Count) & " Frame's [" & Str(ViCut_FFConv_DatSize \ 1000) & " KByte] [T-" & Str(TT) & "]"
    
  329.         End If
    
  330.     Elseif TPrep = 2 Then
    
  331.         If TLFrame <> ViCut_FFConv_Count Then
    
  332.             If TTot < Timer() Then
    
  333.                 TTimeR = Fix(Timer() - TTimeS)
    
  334.                 TT = Format(Int(TTimeR / 3600), "00")
    
  335.                 TTimeR Mod= 3600
    
  336.                 TT += ":" & Format(Int(TTimeR / 60), "00")
    
  337.                 TTimeR Mod= 60
    
  338.                 TT += ":" & Format(TTimeR, "00")
    
  339.                 TTot = Timer() + 1
    
  340.             End If
    
  341.             G_TranscodeMovie_Progress->Value = ViCut_FFConv_Count
    
  342.             G_TranscodeMovie_Progress->Text = "? / " & Str(ViCut_FFConv_Count) & " Frame's [" & Str(ViCut_FFConv_DatSize \ 1000) & " KByte] [T+" & Str(TT) & "]"
    
  343.         End If
    
  344.     End If
    
  345.     Mutexunlock(ViCut_FFConv_ThreadMutex)
    
  346.     Sleep 10, 1
    
  347. Loop
    
  348. Mutexlock(ViCut_FFConv_ThreadMutex)
    
  349. R_FrameCount = ViCut_FFConv_Count
    
  350. R_DatFilesize = ViCut_FFConv_DatSize
    
  351. Mutexunlock(ViCut_FFConv_ThreadMutex)
    
  352. With *G_TranscodeMovie_Progress
    
  353.     .Text = "Success!"
    
  354.     .Max = 1
    
  355.     .Value = 1
    
  356. End With
    
  357. EWH_Window_ThreadSleep(2000)
    
  358. G_TranscodeMovie_Window->Hide()
    
  359. End Sub
    
  360. 
    
  361. 
    
  362. 
    
  363. '##############################################################################################################################################################
    
  364. Sub ViCut_TranscodeMovie_Init()
    
  365. ViCut_FFConv_ThreadMutex = Mutexcreate()
    
  366. G_TranscodeMovie_Window = EWH_Window_Create("ViCut - Transcoding! Please wait...", , , 450, 50)
    
  367. With *G_TranscodeMovie_Window
    
  368.     G_TranscodeMovie_Progress = .Control_Add(EWH_CTL_Progress, 430, 25, 10, 10)
    
  369.     With *G_TranscodeMovie_Progress
    
  370.         .Text = ""
    
  371.         .Max = 0
    
  372.         .Value = 0
    
  373.     End With
    
  374. End With
    
  375. End Sub
    
  376. 
    
  377. 
    
  378. 
    
  379. '##############################################################################################################################################################
    
  380. Function ViCut_TranscodeMovie_GetThumbnailFromFile(V_PathFileName As String, V_Width As Uinteger, V_Height As Uinteger, Byref R_MovieWidth As Uinteger, Byref R_MovieHeight As Uinteger, Byref R_MovieSize As Uinteger) As EWH_Image_Type Ptr
    
  381. Mutexlock(G_FileIOMutex)
    
  382. Dim XFN As Integer = Freefile
    
  383. If Open(V_PathFileName For Binary As #XFN) <> 0 Then Mutexunlock(G_FileIOMutex): Return 0
    
  384. R_MovieSize = Lof(XFN)
    
  385. Close #XFN
    
  386. Kill "temp/thumb.jpg"
    
  387. Mutexunlock(G_FileIOMutex)
    
  388. Shell "ffmpeg -itsoffset -4  -i " & V_PathFileName & " -an -vcodec mjpeg -vframes 1 -f rawvideo temp/thumb.jpg &> /dev/null"
    
  389. Dim TImg As EWH_Image_Type Ptr = EWH_LoadImageFromFile("temp/thumb.jpg")
    
  390. If TImg = 0 Then Return 0
    
  391. R_MovieWidth = TImg->V_Width
    
  392. R_MovieHeight = TImg->V_Height
    
  393. Dim TImgS As EWH_Image_Type Ptr
    
  394. Dim TOutRatioW As Uinteger = 16
    
  395. Dim TOutRatioH As Uinteger = 9
    
  396. Dim PW As Uinteger
    
  397. Dim PH As Uinteger
    
  398. If (V_Width / V_Height) <= TOutRatioW / TOutRatioH Then
    
  399.     PW = V_Width
    
  400.     PH = V_Width / TOutRatioW * TOutRatioH
    
  401. Else
    
  402.     PW = V_Height / TOutRatioH * TOutRatioW
    
  403.     PH = V_Height
    
  404. End If
    
  405. TImgS = EWH_ImageScale(TImg, PW, PH)
    
  406. EWH_ImageDestroy(TImg)
    
  407. Mutexlock(G_FileIOMutex)
    
  408. 'Kill "temp/thumb."jpg
    
  409. Mutexunlock(G_FileIOMutex)
    
  410. Return TImgS
    
  411. End Function
    
  412. 
    
  413.